dudejma Posted January 8, 2012 Share Posted January 8, 2012 Okay, I've read the manual on number_format and I'm not sure if I'm just not grasping it or I'm missing something but I've found out that when I use number format, it only gives me one digit. Like this: I have my code: $newPay = number_format($oldPay + $distance * .40, 2); . I've echoed out $oldPay and it gives the correct number because it comes directly from a database. When I echo out $newPay, I get the same value as $oldPay. Whenever I take away number_format, it still does the same thing. Obviously, I'm pretty new to PHP coding and not very advanced but I'm just not sure what I'm missing. Any help? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/254577-number_format/ Share on other sites More sharing options...
requinix Posted January 8, 2012 Share Posted January 8, 2012 number_format() doesn't quite do what you're expecting. Try round instead. Quote Link to comment https://forums.phpfreaks.com/topic/254577-number_format/#findComment-1305446 Share on other sites More sharing options...
ngreenwood6 Posted January 8, 2012 Share Posted January 8, 2012 number_format() doesn't quite do what you're expecting. Try round instead. He is correct in saying number_format doesn't quite do what youre expecting. It will only keep the decimal points if there are more than two it will limit them to two places. A solution for you would be: $newPay = round($oldPay + $distance * .40, 2); Quote Link to comment https://forums.phpfreaks.com/topic/254577-number_format/#findComment-1305450 Share on other sites More sharing options...
dudejma Posted January 8, 2012 Author Share Posted January 8, 2012 It still gives me the same format. This is what the code is now: $newPay = round($oldPay + $distance * .40, 2); $oldPay = 1,313.40 $distance = 3281 $newPay = 1313.4 Quote Link to comment https://forums.phpfreaks.com/topic/254577-number_format/#findComment-1305452 Share on other sites More sharing options...
ngreenwood6 Posted January 8, 2012 Share Posted January 8, 2012 try this then : $newPay = number_format($oldPay + $distance * .40, 2); $newPay = sprintf("%01.2f", $newPay); Quote Link to comment https://forums.phpfreaks.com/topic/254577-number_format/#findComment-1305453 Share on other sites More sharing options...
dudejma Posted January 8, 2012 Author Share Posted January 8, 2012 $newPay = 1.00 Quote Link to comment https://forums.phpfreaks.com/topic/254577-number_format/#findComment-1305454 Share on other sites More sharing options...
ngreenwood6 Posted January 8, 2012 Share Posted January 8, 2012 That is what you wanted correct? Quote Link to comment https://forums.phpfreaks.com/topic/254577-number_format/#findComment-1305476 Share on other sites More sharing options...
dudejma Posted January 8, 2012 Author Share Posted January 8, 2012 No, I want it to take $oldPay or the amount the pilot already has, and add the $newPay or the amount given for completion of the flight. Quote Link to comment https://forums.phpfreaks.com/topic/254577-number_format/#findComment-1305477 Share on other sites More sharing options...
dudejma Posted January 8, 2012 Author Share Posted January 8, 2012 I figured it out. I guess the server side gets confused when it takes a decimal already formatted with number_format directly from the database. Thanks for the help guys! Quote Link to comment https://forums.phpfreaks.com/topic/254577-number_format/#findComment-1305558 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.