y u no do it?

Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

Starts at just \$1 per CPM or \$0.10 per CPC.

# Is not using float for money that important for only an ecommerce site?

Discussion in 'JavaScript' started by JoshuaEir, Jun 12, 2021.

1. #1
I am still writing an ecommerce site. I am using Javascript, PHP, and Golang. All three work with money, but it is possible that this may be simplified. As far as money goes, I am storing each product's amount in a database, multiplying each cost by quantity, and multiplying each product by a tax rate.
I have read not to use float for representing money because of some approximation problems. However, I don't know if this is really that important for just an ecommerce site. Right now I am rounding up the amounts and I using the following for Javascript:
var roundedTotal = Math.round(total * 100) / 100
and for golang:
SmallerTotalCostString := fmt.Sprintf("%.2f", TotalCostFloat)
As far as php goes, I haven't started these money situations, but I saw this on this site:
number_format
money_format
What is recommended so that everything works well, and everything works well together. I would think a computer would be just as great as a cash register! Maybe there are standards?
Thank you!

Edit: I have read to use just cents, but there are still problems multiplying by a decimal.

Last edited: Jun 12, 2021
Jun 12, 2021 IP
2. ### mmerlinnProminent Member

Messages:
2,988
714
7
Trophy Points:
320
#2

However, in 40 years of programming I have found that STORING numbers as STRINGS, then converting to values as needed eliminates most but not all problems related to rounding. Will that work for you? No clue, but it might be worth a try.

Since you are dealing with monetary numbers, I would store them without a decimal point with the implicit understanding that the decimal point is ALWAYS x digits from the right, that is, 2 in the case of American dollars.

Further, I would never store anything that can be calculated on the fly. For example I would store the base monetary amount and the tax rate, but NEVER the resulting amount when the two are multiplied together.

Jun 12, 2021 IP
3. ### JoshuaEirMember

Messages:
54
8
0
Trophy Points:
28
#3
"I have found that STORING numbers as STRINGS, then converting to values as needed eliminates most but not all problems related to rounding."

Thanks. Well, this isn't a real ecommerce site, it is just for practice. However I wanted something that is always acceptable. Is it okay to have any rounding errors when dealing with money? I'm not sure, maybe no? Or, maybe, they are all acceptable because it is done with rules/calculations?

Last edited: Jun 12, 2021
Jun 12, 2021 IP
4. ### JoshuaEirMember

Messages:
54
8
0
Trophy Points:
28
#4
mmerlinn, how does this sound...Change both the cost and the tax rate to no decimals, multiply, and than move the decimal point?

Jun 13, 2021 IP
5. ### mmerlinnProminent Member

Messages:
2,988
714
7
Trophy Points:
320
#5
In general, rounding errors are NEVER acceptable when dealing with money.

Since I do not have much of a grasp on JS, I don't know if there is any advantage to using decimal points in CALCULATIONS or not. I seriously doubt that would be the case. You will get the same rounding errors both ways.

The main thing to consider is that, when COMPARING the NUMERICAL results of calculations, rounding errors will drive you up a wall since two results that LOOK the same are NOT the same. That is the main reason I store numbers as strings since what you see with strings is exactly what you get.

Jun 13, 2021 IP
6. ### JoshuaEirMember

Messages:
54
8
0
Trophy Points:
28
#6
"rounding errors will drive you up a wall since two results that LOOK the same are NOT the same."

I don't understand, how do they look the same when they are not, mmerlinn?

Jun 13, 2021 IP
7. ### mmerlinnProminent Member

Messages:
2,988
714
7
Trophy Points:
320
#7
Because your computer stores numbers differently than they are displayed on your screen.

Do a test. If you test for true/false with 0.1 + 0.2 == 0.3 your computer will come back with FALSE, even though it LOOKS like it should be TRUE.

Jun 13, 2021 IP

Messages:
54