Why Is My Number Value Changing Using Number_Format()

Why is my number value changing using number_format()?

number_format(), as stated by the PHP manual, converts a float into a string with the thousands groups with commas.

$number = '1562798794365432135246';
var_dump($number); // string(22) "1562798794365432135246"
var_dump(number_format($number)); // string(29) "1,562,798,794,365,432,233,984"

If you're trying to cast a string to an integer, you can do so like this:

$number = (int) $number;

However, be careful, since the largest possible integer value in PHP is 2147483647 on a 32-bit system and 9223372036854775807 on a 64-bit system. If you try to cast a string like the one above to int on a 32-bit system, you'll assign $number to 2147483647 rather than the value you intend!

The number_format() function takes a float as an argument, which has similar issues. When you pass a string as an argument to number_format(), it is internally converted to a float. Floats are a little more complicated than integers. Instead of having a hard upper bound like an integer value, floats progressively lose precision in the least significant digits, making those last few places incorrect.

So, unfortunately, if you need to format long strings like this you'll probably need to write your own function. If you only need to add commas in the thousands places, this should be easy - just use strlen and substr to get every set of three characters from the end of string and create a new string with commas in between.

PHP number_format displaying incorrectly

You're seeig a different number because the number you have provided is large enough to cause an overflow on your system. Try with a smaller number

<?php
$num = 9876543210123;
$result = number_format($num);
echo $result; // will show as expected, with formatting

Even if you don't use number_format on that number, your number will still cause overflow. Try this

$num = 9876543210123456789; 
echo $num; // not what you see above

Also see:

What's the maximum size for an int in PHP?

PHP Integers and Overflow

How to make number_format() not to round numbers up

number_format will always do that, your only solution is to feed it something different:

$number = intval(($number*100))/100;

Or:

$number = floor(($number*100))/100;

PHP - number_format issues

The second argument of number_format is the number of decimals in the number. The easiest way to find that out is probably to treat it as a string (as per this question). Traditional US format is default behaviour, so you don't need to specify remaining arguments.

$num_decimals = strlen(substr(strrchr($number, "."), 1));
$formatted_number = number_format($number, $num_decimals);

How do I convert output of number_format back to numbers in PHP?

You could remove any character that is not a digit or a decimal point and parse that with floatval:

$number = 1200.00;
$parsed = floatval(preg_replace('/[^\d.]/', '', number_format($number)));
var_dump($number === $parsed); // bool(true)

And if the number has not . as decimal point:

function parse_number($number, $dec_point=null) {
if (empty($dec_point)) {
$locale = localeconv();
$dec_point = $locale['decimal_point'];
}
return floatval(str_replace($dec_point, '.', preg_replace('/[^\d'.preg_quote($dec_point).']/', '', $number)));
}

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.

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);


Related Topics



Leave a reply



Submit