PHP Number Format

Show a number to two decimal places

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.

How can I format this number correctly using PHP?

The problem is that first the price "154,95" is converted to number as 154, and after that number_format() starts to do his job.
Either you have to store in the database the price as 154.95, or you have to replace the character "," with "." before calling number_format().
Example:

<?php
$a = "159.95";
$b = "12345";
$a_number = str_replace(",", ".", $a);
$b_number = str_replace(",", ".", $b);

echo number_format($a_number,2,",","."), "\n";
echo number_format($b_number,2,",","."), "\n";
?>

And the output is:

159,95

12.345,00

Number format PHP european format (= 3.213.124,4355) without 0's in decimal

Found an answer by myself (with using/changing code that I found in comments on number_format() ):

function number_format_unchanged_precision($number, $dec_point='.', $thousands_sep=','){
if($dec_point==$thousands_sep){
trigger_error('2 parameters for ' . __METHOD__ . '() have the same value, that is "' . $dec_point . '" for $dec_point and $thousands_sep', E_USER_WARNING);
// It corresponds "PHP Warning: Wrong parameter count for number_format()", which occurs when you use $dec_point without $thousands_sep to number_format().
}
$decimals = strlen(substr(strrchr($number, "."), 1));

return number_format($number, $decimals, $dec_point, $thousands_sep);
}

and here also a function that includes rounding, but doesn't add the useless 0's: (I think the normal number_format() function should work like this...)

function number_format_without_zeroindecimal($number, $maxdecimal, $dec_point='.', $thousands_sep=','){
if($dec_point==$thousands_sep){
trigger_error('2 parameters for ' . __METHOD__ . '() have the same value, that is "' . $dec_point . '" for $dec_point and $thousands_sep', E_USER_WARNING);
// It corresponds "PHP Warning: Wrong parameter count for number_format()", which occurs when you use $dec_point without $thousands_sep to number_format().
}
$decimals = strlen(substr(strrchr(round($number,$maxdecimal), "."), 1));

return number_format($number, $decimals, $dec_point, $thousands_sep);
}

Number format with a variable decimal place

rtrim(number_format(1234.234, 3),'.0');
rtrim(number_format(1234, 3),'.0');

PHP number format with integer format

Just do a loose comparison of $price cast as integer against $price, if they match (ie. it's a whole number), you can format to 0 decimal places:

number_format($price, ((int) $price == $price ? 0 : 2), '.', ',');

PHP number_format(): rounding numbers and then formatting as currency

As specified in the documentation, number_format returns a string value, you can't reuse it as a number.
Use the function round() to round your number, if you want to round it to the direct upper integer use ceil() instead.

number_format(round(12345.6789), 2);

Prevent number_format from rounding numbers - add commas and keep the decimal point - PHP

In case what you meant by they vary in length is related to the decimal part, take a look at this code:

function getLen($var){
$tmp = explode('.', $var);
if(count($tmp)>1){
return strlen($tmp[1]);
}
}
$var = '1000000.255555'; // '1000000.25'; // '1000000';
echo number_format($var, getLen($var));


Some tests

  • Output for 1000000.255555:

1,000,000.255555


Output for 1000000.25:

1,000,000.25


Output for 1000000:

1,000,000


It counts how many chars there are after the . and uses that as argument in the number_format function;

Otherwise just use a constant number instead of the function call.


And some reference...


From the manual -> number_format():

string number_format ( float $number [, int $decimals = 0 ] )

And you can see in the description that

number

The number being formatted.

decimals

Sets the number of decimal points.

And a bit more:

[...]If two parameters are given, number will be formatted with decimals
decimals with a dot (".") in front, and a comma (",") between every
group of thousands.

How to change format number in PHP

You can use number_format() function here:
www.w3schools.com/php/func_string_number_format.asp

See last example here where they set for as thousands separator:
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_func_string_number_format

Your code updated as below I believe:

update_field('views', number_format($count,0,",","."));

numberformat error with bigs numbers in php

A couple notes.

"A non well formed numeric value encountered" is a Notice, not an Error.

I don't believe 1300.75 works for you. The reason I don't believe this is you are only giving number_format two parameters. You are receiving that notice because number_format is formatting your number with a thousands separator ",".

$final_amount = number_format($final_amount, 2, ".", "") * 100;

should do the trick to remove that notice.

Formatting a number

This is quite easy with number_format:

number_format(value, number_of_decimals, "decimal_separator", "thousand_separator")

Something like this:

echo number_format(19809.1, 2, ",", " ");

This tells the number to have 2 decimals, comma , as decimal separator and a whitespace as thousand separator. Output will be:

19 809,10

Other examples:

echo number_format(19809.1, 0, "", ".");
> 19.809
> no decimal and . as thousand separator

echo number_format(19809.1, 3, ".", ",");
> 19,809.100
> 3 decimals, comma as thousand separator and dot as decimal separator


Related Topics



Leave a reply



Submit