PHP rounding problem (5.2.3)? -


I'm thinking that I got an issue with the sphere in PHP, especially 5.2.3 (I'm not sure Other versions of this time):

  $ t = 0; $ Tax amount = (5.000 / 100) * 0.7; $ T + = $ tax amount; Var_dump ($ t); // Flat (0.035) var_dump (round ($ t, 2)); // float (0.03) var_dump (number_ format ($ t, 2)); // 0.04 "0.04" for 0.035 should I round for 0.04 or am I just crazy?  

Edit

Thxs, I think I would have to do this instead:

  $ t = 0; $ Tax amount = BCMUL (BCDIV (5.000, 100, 3), 0.7, 3); $ T + = $ tax amount; Var_dump ($ t); // Flat (0.035) var_dump (round ($ t, 2)); // float (0.04) var_dump (number_ format ($ t, 2)); // string (4) "0.04"  

what works.

BTW, I calculate the tax in the shopping cart. Order total is 0.70 (70 cents) and tax is 5%.

Edit

Thxs, to show where the problem is:

  printf ('%' 18f ', 5.000 / 100 * 0.7);  

Floating is bad.

Excerpt:

Then do not believe the number of trusting numbers on the last issue, and never compare the floating point numbers for equality . If high precision is required, more functions are available.

If you want to know why and how do I look:


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -