ChatGPT解决这个技术问题 Extra ChatGPT

Show a number to two decimal places

What's the correct way to round a PHP string to two decimal places?

$number = "520"; // It's a string from a database

$formatted_number = round_to_2dp($number);

echo $formatted_number;

The output should be 520.00;

How should the round_to_2dp() function definition be?

This really isn't "rounding". Rounding reduces precision. It will not add decimal places to a number that doesn't have them. Based on many of the answers and comments, it seems like people are thinking that rounding is something it isn't. round is a math function, and this is just a formatting problem.

C
Codemwnci

You can use number_format():

return number_format((float)$number, 2, '.', '');

Example:

$foo = "105";
echo number_format((float)$foo, 2, '.', '');  // Outputs -> 105.00

This function returns a string.


round() would be better for it's readability and simplicity too, but for anything critical be aware that it has been known to be buggy
@ÁlvaroG.Vicario round doesn't solve the OP's problem here. round("520", 2) returns the float 520, and echoing that will of course not show it to 2 decimal places. For the OP's purpose - showing an integer to 2 decimal places by padding it with trailing zeroes - you need a number formatting function, not a rounding function.
All the other answers are limited to decimal values with a dot notation. When you need to get 520,00 instead of 520.00, this simply works.
if you want to limit only float but not int then bcdiv($number,1,2)
I think you should use bcadd(0,$yournumber,2) it will give you perfect result.
J
Jacopo Pace

Use round() (use if you are expecting a number in float format only, else use number_format() as an answer given by Codemwnci):

echo round(520.34345, 2);   // 520.34
echo round(520.3, 2);       // 520.3
echo round(520, 2);         // 520

From the manual:

Description: float round(float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]]); Returns the rounded value of val to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).

...

Example #1 round() examples Example #2 mode examples


@khuderm round($number, 2); doesn't work. I want 520.00 not 520. Can't believe round doesn't do that by default.
It doesn't even answer the OP correctly but has over 100 votes up!
I want use number format but when 100.00 will return 100. How is it?
but i need .93 if the number 123.93 @SomnathMuluk
@RobSedgwick many poeple are searching for function to trim the remaining part after 3.333333333 to be 3.33 etc.
M
Marc B

Alternatively,

$padded = sprintf('%0.2f', $unpadded); // 520 -> 520.00

So what if you want 520,00? I guess the other answer works better in that case.
^no the other answer will not give two decimal places for an int
other reasons you may want this is you dont want rounding... or in my cause SQL floats returned are not treated the same as normal floats which is a PHP math bug
@SPRBRN - number_format has an option to specify decimal separator character.
a
aksu

http://php.net/manual/en/function.round.php

e.g.

echo round(5.045, 2);    // 5.05

echo round(5.055, 2);    // 5.06

Round will do the exact functionality that Rich is wanting, but number_format() would be more interesting to look into for all kinds of numerical formatting.
This method will perform rounding as well over just forcing the digits ".00" on the end.
I'd tried round, but it doesn't work - it won't add the extra .00.
round(520, 2) returns 520, not 520.00 as required by @Rich
Update for PHP 7.4+ since this answer is very old. Since round returns a float it can only be guaranteed to return a number with at most one decimal place (ie, it returns something that is not an integer, with the minimum number of decimal places possible to represent the number). So round(1,2) (or any level or precision) returns '1.0', because '1.00' would not be a valid float. To make numbers behave like strings, you need to use string manipulation functions. (Previous versions of PHP may have acted differently/less logically)
L
Leigh

Try:

$number = 1234545454; 
echo  $english_format_number = number_format($number, 2); 

The output will be:

1,234,545,454.00

That works, but the output would be a string and not a number.
if you want to use number_format at least make it number_format($number, 2, '.', '')
@Ethan - correct, this Q&A is about formatting a number as a string, with a specified number of decimal digits.
P
Peter Mortensen

Use the PHP number_format() function.

For example,

$num = 7234545423;
echo number_format($num, 2);

The output will be:

7,234,545,423.00

P
Peter Mortensen

You can use the PHP printf or sprintf functions:

Example with sprintf:

$num = 2.12;
echo sprintf("%.3f", $num);

You can run the same without echo as well. Example: sprintf("%.3f", $num);

Output:

2.120

Alternatively, with printf:

echo printf("%.2f", $num);

Output:

2.124

p
powtac

Another more exotic way to solve this issue is to use bcadd() with a dummy value for the $right_operand of 0.

$formatted_number = bcadd($number, 0, 2);

Note that bc* functions do not round up, they always round down. For example, bcadd("1.0999","0.0",1) yields "1.0", not "1.1".
BC Math functions actually just stop processing at $scale fractional digits. This equals rounding towards zero.
P
Peter Mortensen
bcdiv($number, 1, 2) // 2 varies for digits after the decimal point

This will display exactly two digits after the decimal point.

Advantage:

If you want to display two digits after a float value only and not for int, then use this.


This is the most universal solution if you have bcdiv (it's a stock but non-default package on centos6).
P
Pang
$retailPrice = 5.989;
echo number_format(floor($retailPrice*100)/100,2, '.', ''); 

It will return 5.98 without rounding the number.


Only this can also do the job floor($retailPrice * 100) / 100; no need of number format then.
V
Vinay

For conditional rounding off ie. show decimal where it's really needed otherwise whole number

123.56 => 12.56

123.00 => 123

$somenumber = 123.56;

$somenumber = round($somenumber,2);

if($somenumber == intval($somenumber))
{
    $somenumber = intval($somenumber);
}

echo $somenumber; // 123.56


$somenumber = 123.00;

$somenumber = round($somenumber,2);

if($somenumber == intval($somenumber))
{
    $somenumber = intval($somenumber);
}

echo $somenumber; // 123    

P
Peter Mortensen

Use the PHP number_format() function.


P
Peter Mortensen

Here I get two decimals after the . (dot) using a function...

function truncate_number($number, $precision = 2) {

    // Zero causes issues, and no need to truncate
    if (0 == (int)$number) {
        return $number;
    }

    // Are we negative?
    $negative = $number / abs($number);

    // Cast the number to a positive to solve rounding
    $number = abs($number);

    // Calculate precision number for dividing / multiplying
    $precision = pow(10, $precision);

    // Run the math, re-applying the negative value to ensure
    // returns correctly negative / positive
    return floor( $number * $precision ) / $precision * $negative;
}

Results from the above function:

echo truncate_number(2.56789, 1); // 2.5
echo truncate_number(2.56789);    // 2.56
echo truncate_number(2.56789, 3); // 2.567

echo truncate_number(-2.56789, 1); // -2.5
echo truncate_number(-2.56789);    // -2.56
echo truncate_number(-2.56789, 3); // -2.567

New Correct Answer

Use the PHP native function bcdiv

echo bcdiv(2.56789, 1, 1);  // 2.5
echo bcdiv(2.56789, 1, 2);  // 2.56
echo bcdiv(2.56789, 1, 3);  // 2.567
echo bcdiv(-2.56789, 1, 1); // -2.5
echo bcdiv(-2.56789, 1, 2); // -2.56
echo bcdiv(-2.56789, 1, 3); // -2.567

Thanks for the update. Works great for longitude & latitude.
P
Peter Mortensen

I make my own.

$decimals = 2;
$number = 221.12345;
$number = $number * pow(10, $decimals);
$number = intval($number);
$number = $number / pow(10, $decimals);

Its nice to see some of the maths needed to do this. Although using a built in function is preferred for me. Theres no point re-inventing the wheel.
Sorry, but round(), as it says, round de number, and sometimes, i do not want to round.
T
ThomasCanBeNormal

use roud(yourValue,decimalPoint) or number_format(yourValue,decimalPoint);

number_format() return value as string like this type 1,234.67. so in this case you can not use it for addition or any calculation. if you try then you have to deal with Number Format Error...

In this case round(121222.299000000,2) will be better option. The result would be 121222.29 ...


P
Peter Mortensen

round_to_2dp is a user-defined function, and nothing can be done unless you posted the declaration of that function.

However, my guess is doing this: number_format($number, 2);


From your first paragraph, I think you've misunderstood the question. round_to_2dp($number); was meant as pseudocode; the OP was asking for somebody to tell him what to replace that expression with.
T
Tunaki
$twoDecNum = sprintf('%0.2f', round($number, 2));

The rounding correctly rounds the number and the sprintf forces it to 2 decimal places if it happens to to be only 1 decimal place after rounding.


K
Kampai
$number = sprintf('%0.2f', $numbers); // 520.89898989 -> 520.89

This will give you 2 number after decimal.


No it won't. I don't know when this post was written but if you try it with your own example, at least on my machine :-) , it will output 520.90. Meaning, Rounding WILL occur. Be careful!
P
Peter Mortensen

If you want to use two decimal digits in your entire project, you can define:

bcscale(2);

Then the following function will produce your desired result:

$myvalue = 10.165445;
echo bcadd(0, $myvalue);
// result=10.11

But if you don't use the bcscale function, you need to write the code as follows to get your desired result.

$myvalue = 10.165445;
echo bcadd(0, $myvalue, 2);
// result=10.11

To know more

BC Math Functions

bcscale


G
Glorious Kale

Adding to other answers, since number_format() will, by default, add thousands separator.

To remove this, do this:

$number = number_format($number, 2, ".", "");

P
Peter Mortensen

Number without round

$double = '21.188624';
echo intval($double) . '.' . substr(end(explode('.', $double)), 0, 2);

P
Peter Mortensen

Here's another solution with strtok and str_pad:

$num = 520.00
strtok(round($num, 2), '.') . '.' . str_pad(strtok('.'), 2, '0')

K
Kareem

Choose the number of decimals

Format commas(,)

An option to trim trailing zeros

Once and for all!

function format_number($number,$dec=0,$trim=false){
  if($trim){
    $parts = explode(".",(round($number,$dec) * 1));
    $dec = isset($parts[1]) ? strlen($parts[1]) : 0;
  }
  $formatted = number_format($number,$dec); 
  return $formatted;
}

Examples

echo format_number(1234.5,2,true); //returns 1,234.5
echo format_number(1234.5,2);      //returns 1,234.50
echo format_number(1234.5);        //returns 1,235

p
paulalexandru

In case you use math equation like I did you can set it like this:

{math equation="x + y" x=4.4444 y=5.0000 format="%.2f"}

R
Rehan Anis

That's the same question I came across today and want to round a number and return float value up to a given decimal place and it must not be string (as returned from number_format) the answer is

echo sprintf('%.' . $decimalPlaces . 'f', round($number, $decimalPlaces));


echo sprintf() is an antipattern. It is more sensibly written as printf() only.