Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Snapey's avatar
Level 122

int conversion issue

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?

0 likes
34 replies
Sinnbeck's avatar

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);
Snapey's avatar
Level 122

number_format returns a string, but I guess that is fine for a model update.

jlrdw's avatar

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.

Snapey's avatar
Level 122

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
automica's avatar

@snapey I cant get over how broken that is:

>>> intval("8.03"*100)
=> 802
>>> "8.03"*100
=> 803.0
>>> 
Snapey's avatar
Level 122

The php website has comments documenting the same issue from 14 years ago

SarwarAhmed's avatar

$num = number_format("8.03" * 100, 0);

$int = (int)$num;

bugsysha's avatar

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?

Snapey's avatar
Level 122

I ended up using strval instead of intval, but give the answer to @sinnbeck

1 like
bugsysha's avatar

Usually people would select their own answer as best one. Nice pick.

laracoft's avatar

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.

artcore's avatar

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

1 like
Snapey's avatar
Level 122

@artcore

observe tinker;

>>> floatval('8.03'*100)
=> 803.0
>>> intval(floatval('8.03'*100))
=> 802
>>> strval('8.03'*100)
=> "803"
>>>

column type is int

Snapey's avatar
Snapey
OP
Best Answer
Level 122

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.

2 likes
automica's avatar

@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.

automica's avatar

@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.

jlrdw's avatar

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.

Snapey's avatar
Level 122

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
Snapey's avatar
Level 122

@jlrdw

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
>>>
jlrdw's avatar
$formattednumber = number_format($mynumber, 0, ".", '');

Works,

jlrdw's avatar
        $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
artcore's avatar

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

Please or to participate in this conversation.