This works for me (tested with a bunch of numbers and it works every time). I usenumber_formatfor rounding in a statistics system I am maintaining, and it is good for float handling :)
number_format("8.03" * 100, 0);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Can anyone help with this;
>>> intval("8.03"*100)
=> 802
>>> "8.03"*100
=> 803.0
>>>
I have a price coming back from a form, represented by "8.03" This is what the user has entered as the price of an item.
I want to store in pence, ie 803
intval function returns 802
What is the safe way to convert this number to an integer?
This works for me (tested with a bunch of numbers and it works every time). I usenumber_formatfor rounding in a statistics system I am maintaining, and it is good for float handling :)
number_format("8.03" * 100, 0);
number_format returns a string, but I guess that is fine for a model update.
I don't know if this applies, but I import bank data Checking for non-profit,
$amt = (float) $array[$i][3];
$namt = number_format($amt, 2, '.', '');
Works fine. Never used intval.
I like casting to number format first, then doing any math calculations.
Some other problematic values
150.20
19.99 * this is a biggie for ecommerce
This also works ok
>>> strval("19.99"*100)
=> "1999"
>>> strval("8.03"*100)
=> "803"
>>> strval("150.20"*100)
=> "15020"
and for integer output
>>> intval(strval("8.03"*100))
=> 803
@snapey I cant get over how broken that is:
>>> intval("8.03"*100)
=> 802
>>> "8.03"*100
=> 803.0
>>>
The php website has comments documenting the same issue from 14 years ago
blimey. no rush then :P
$num = number_format("8.03" * 100, 0);
$int = (int)$num;
@sarwarahmed I would flip that, do the cast first.
I’ve always used ceil() for that. Haven’t had that issue in a while. Can you describe the scenario? Are you storing prices in cents?
I did describe the issue
@snapey Try http://php-decimal.io/#introduction?
802 is happening because of the use of float/double. It happens in other languages such as VB/C# too.
Decimals will handle it the like the way we were taught maths in school.
Don’t know how I’ve missed that.
I ended up using strval instead of intval, but give the answer to @sinnbeck
Usually people would select their own answer as best one. Nice pick.
It is good practice to give credit where it is due, but also make sure best answer has a solution to the question (to help people who land here via search).
Normally, I ask the closest/most helpful answer to edit and add in the best solution.
How is number_format() useful for db storage? If your value is over a 1000 you will get the separator rendering it useless for calculations. If you store as int, you should have a db column INT. In your case validate a float and use floatval() *100 as @jlrdw mentioned to get he INT.
Unless I misread the post on this late Sunday eve ;) but wanted to comment on not using formatting for storage, that's for presenting imho
observe tinker;
>>> floatval('8.03'*100)
=> 803.0
>>> intval(floatval('8.03'*100))
=> 802
>>> strval('8.03'*100)
=> "803"
>>>
column type is int
Thanks to @artcore answer, I'm going to have to change best answer because number format introduces thousands separator which breaks storage in an int column in the database.
but @artcore answer is also incorrect because floatval does not avoid the issue with specific float numbers.
strval seems to be the safest solution. Sorry guys.
@snapey number formats second argument is for the thousands separator
number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," ) : string
if you pass it as empty
number_format(100030, 2,'.',''); // returns 100030.00
you don't get the comma.
You can set decimal to 0.
@automica why, when strval() works with no messing about?
@snapey I didn't say for you to use it, just challenging your:
I'm going to have to change best answer because number format introduces thousands separator which breaks storage in an int column in the database.
Just testing / playing with it.
$somenumber = "8.03";
$floatnumber = (float) $somenumber;
$mynumber = $floatnumber * 100;
echo $mynumber;
echo "<br>";
$formattednumber = number_format($mynumber, 0);
echo $formattednumber;
echo "<br>";
$intnumber = (int) $formattednumber;
echo $intnumber;
die;
output:
803
803
803
Last one being an int.
I usually write a helper to return needed values.
gets even crazier...
>>> $price = (float) '8.03'
=> 8.03
>>> (int) $price*100
=> 800
>>>
since (int) works on $price before the *100
>>> (int) ($price*100)
=> 802
Your code with a larger number
>>> $somenumber = "15.20";
=> "15.20"
>>> $floatnumber = (float) $somenumber;
=> 15.2
>>> $mynumber = $floatnumber * 100;
=> 1520.0
>>> $formattednumber = number_format($mynumber, 0);
=> "1,520"
>>> $intnumber = (int) $formattednumber;
=> 1
>>>
$formattednumber = number_format($mynumber, 0, ".", '');
Works,
$somenumber = "15.20";
$floatnumber = (float) $somenumber;
$mynumber = $floatnumber * 100;
echo $mynumber;
echo "<br>";
$formattednumber = number_format($mynumber, 0, ".", '');
echo $formattednumber;
echo "<br>";
$intnumber = (int) $formattednumber;
echo $intnumber;
die;
output
1520
1520
1520
so does strval('15.20' * 100)
What gets stored if "they" input more decimals?
strval('8.0356' * 100)
^ "803.56"
And be careful with the input. It can break the strval() I assume you're using a type=number input but you never know.
strval('15.20 ' * 100)
"A non well formed numeric value encountered" due to the space
some fun trying to make it fool proof
$value = '15.20 ';
$value = preg_replace('/[^\d.]/', '', $value);
$number = strtok($value, '.');
$decimal = substr(str_replace("$number.", '' , $value), 0, 2);
$value = $number . $decimal;
Please or to participate in this conversation.