fazzfarrell Posted May 31, 2007 Share Posted May 31, 2007 My code <?php //Rob Farrell calculate cost - % $english_format_number = number_format($number); $number1 = $HTTP_SESSION_VARS["icPoles"]->col("Total"); if (isset($_SESSION['MM_Username'])) { $number = ($number1 / $row_rsUser['Percent'] * 100); }else{ $number = $number1; } echo number_format ($number, 2, '.', ' '); ?> gives the result Total: £789.23 You Pay (including Delivery) £ 671.69 which is correct, if I go over a £1000 it gives this for example: Total: £1,249.23 You Pay (including Delivery) £ 0.85 Any one see why? Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/ Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 You're probably using a formatted number for calculation. Either withhold the formatting before calculations, or use something like str_replace(',','',$num) to get rid of the commas. Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/#findComment-265577 Share on other sites More sharing options...
fazzfarrell Posted May 31, 2007 Author Share Posted May 31, 2007 May be I did not explain propery, if the price is under a thousand the 'you pay' price is correct if the total goes over a thousand is takes the price right down from £1,249.23 to you pay: 0.85 Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/#findComment-265595 Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 Uh, yeah. That's probably why it's not working. The only difference between a number under a thousand and over that might cause that kind of error would be a comma. Despite looking like a number to you or me, computers don't typically interpret numbers containing commas as numbers, but as strings. If you know how PHP tries to convert strings to numbers, you can predict that it will try to convert the string "1,000.00" to a number by taking all the leftmost numbers until it hits a non-number or non-decimal, in this case the comma. That means PHP converts "1,000.00" to 1. Seriously, check your variables within your calculations; see if there isn't a comma in the value. Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/#findComment-265604 Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 Would you like an example? <?php $value = "1,000.00"; echo $value,"\n"; echo intval($value),"\n"; echo floatval($value),"\n"; echo $value+1,"\n"; ?> (output:) 1,000.00 1 1 2 Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/#findComment-265607 Share on other sites More sharing options...
fazzfarrell Posted May 31, 2007 Author Share Posted May 31, 2007 Hi thanks I have looked at the example and I have gone through loads of reference to try and work this out I must say I am at a loss! Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/#findComment-265624 Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 Do this for me. Execute this code in place of your posted code above and post the output. <?php $english_format_number = number_format($number); $number1 = $HTTP_SESSION_VARS["icPoles"]->col("Total"); if (isset($_SESSION['MM_Username'])) { echo "\$number1 = $number1<br>\n\$row_rsUser['Percent'] = $row_rsUser[Percent]<br>\n"; $number = ($number1 / $row_rsUser['Percent'] * 100); } else { echo "\$number1 = $number1<br>\n"; $number = $number1; } echo number_format ($number, 2, '.', ' '); ?> Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/#findComment-265632 Share on other sites More sharing options...
fazzfarrell Posted May 31, 2007 Author Share Posted May 31, 2007 i get this: £ $number1 = 1,249.23 $row_rsUser['Percent'] = 117.5 0.85 Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/#findComment-265638 Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 i get this: £ $number1 = 1,249.23 $row_rsUser['Percent'] = 117.5 0.85 So it's like I suspected, $number1 is a string containing non-number characters, and you'll have to remove them before using it in a calculation (or at least if expecting it to behave like 1249.23). Make this change: if (isset($_SESSION['MM_Username'])) { $number = floatval(str_replace(',','',$number1)) / $row_rsUser['Percent'] * 100; Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/#findComment-265641 Share on other sites More sharing options...
fazzfarrell Posted May 31, 2007 Author Share Posted May 31, 2007 im still getting a result of 0.85 ? Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/#findComment-265646 Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 What do you want to get? I get this: <?php $number1 = '1,249.23'; $number = floatval(str_replace(',','',$number1)) / 117.5*100; echo number_format ($number, 2, '.', ' '),"\n"; ?> output: 1 063.17 Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/#findComment-265655 Share on other sites More sharing options...
fazzfarrell Posted May 31, 2007 Author Share Posted May 31, 2007 My mistake, totaly missed something - works fine thanks, just looking and working out exacly what you have done for further referance thanks Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/#findComment-265659 Share on other sites More sharing options...
Daniel0 Posted May 31, 2007 Share Posted May 31, 2007 The number must be formatted accordingly to PHP's syntax. I.e. no thousand separator and . (dot) as decimal separator. Quote Link to comment https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/#findComment-265661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.