String replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
asai
Forum Commoner
Posts: 43
Joined: Tue May 04, 2010 6:24 am
Location: Norway

String replace

Post by asai »

Hi,

I am trying to make a string to a int.

Heres an example:

Code: Select all

<?php
$a1 = '567,89';
$a1 = (int)str_replace(',', '.', $a1);
echo $a1;
?>
Expected output is 567.89. However the output is 567.
How can I get the result I want?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: String replace

Post by requinix »

Integers don't have fractional parts. That's what makes them integers.

Try (float) instead.
asai
Forum Commoner
Posts: 43
Joined: Tue May 04, 2010 6:24 am
Location: Norway

Re: String replace

Post by asai »

Yes ofcource. I should have known this.
Done it a lot of times in java programming.
Thanks for the help. :)
Post Reply