Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with number format

Status
Not open for further replies.

hos2

Programmer
Joined
May 6, 2002
Messages
418
Location
NL
I'm building an order system where I want to show the money in decimals.

this works fine in some cases but in a few other cases it doesn't work like the way I do

I multiply the quantity with the cost
like
$totaal=number_format($orlqty*$orlcost,2,',','.');


in some cases the value is what it should be like 1500,00
but in some other cases it's divided by 1000 so I see 1,5

strange thing is that when I put in a value like 300 I get 300,00 but when I enter 1500 I get 1,5
and in other cases when I put in a value greater than 1000 it will work fine as well

very confusing :(




 
Be aware that number_format() returns a string.
The two factors in your multiplication must both be numeric. If you apply formatting to either before using them in the multiplication you'll get strange results since only portions of the string are used.
That's what it looks like to me, but there is not enough of your code to tell for sure.
 
ah I see, that solves the problem quickly.
Code:
$orlqty=$row["orlqty"];
$orlcost=number_format($row["orlcost"],2,',','.');
$totaal=number_format($orlqty*$orlcost,2,',','.');

removing the number_format from the orlcost solved the problem. but still strange that the behaviour is very fuzzy and not consequent


 
The behavior is consequent. Let's play through 2 cases:
Code:
Quantity is 1, cost is 300
$orlcost=number_format($row["orlcost"],2,',','.');
-> this yields 300,00
$totaal=number_format($orlqty*$orlcost,2,',','.');
-> the multiplication is 1 * 300

Quantity is 1, cost is 1500
$orlcost=number_format($row["orlcost"],2,',','.');
-> this yields 1.500,00
$totaal=number_format($orlqty*$orlcost,2,',','.');
-> the multiplication is 1 * 1.5

No fuzzyness at all. Just straight logic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top