1
$result = number_format($x, 2, '.', ','); //Will do the following correctly
115255 = 115,225.00
115255.4 = 115,225.40
115255.40 = 115,225.40
115255.455 = 115,255.46

But I need when the user enters more than 2 digits after the decimal, not to cut them into 2 decimals only and to use it as it is...

115255.455 = 115,255.455
115255.4557 = 115,255.4557

Can I do something like that?

if($x == number_format($x, 3)) //I will do it in while loop later, lets test 3 now
    $result = number_format($x, 3, '.', ',');
else $result = number_format($x, 2, '.', ',');

The previous if condition never works, else only works

1
  • Hm, why you just dont separate the number and format only the integer, and then append the fraction Commented Nov 9, 2013 at 5:04

1 Answer 1

1

One not traditional way to do this:

$parts = explode(".", $x);
$integerPart = number_format($parts[0], 0, '', ',');
$result = $integerPart.".".$parts[1];
Sign up to request clarification or add additional context in comments.

2 Comments

yup, if it's possible :)
can I explode it to use it like $parts[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.