How to convert point as decimal separator to comma?

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
myplaces
Forum Newbie
Posts: 11
Joined: Wed Nov 12, 2008 3:25 pm

How to convert point as decimal separator to comma?

Post by myplaces »

I have form where to write one number and other form to write one more number. And I need for example multiply both numbers.
If someone enters number with comma (for example 200,51) php script thinks that number is 200 (without ,55)
how to convert comma to number?
NoorAdiga
Forum Newbie
Posts: 5
Joined: Thu Dec 11, 2008 6:10 pm

Re: How to convert point as decimal separator to comma?

Post by NoorAdiga »

U need two steps to do this ... the first by explode the number entered by the user into an array (if there is multiple commas then the make sure that the number type is string otherwise u will get error msg! )
then implode this number again without any spaces or commas and u will just a number without commas

Code: Select all

$number= "123,123,123";
$number= explode(',',$number);
$number= implode ($number);
echo "now the number is $number<br>";
 
and I tested the code for u and it gave me this output:
now the number is 123123123


Regards,
Noor
myplaces
Forum Newbie
Posts: 11
Joined: Wed Nov 12, 2008 3:25 pm

Re: How to convert point as decimal separator to comma?

Post by myplaces »

Thanks.
I have found one more solution
<?php $output = floatval(ereg_replace("[^-0-9\.]","",$input)); ?>

But the next problem
If i enter for example 22.5
i get displayed 22.5
but i need 22.50 with zero.
round ($output, 2) does not help....


Found solution

<?php printf("%.2f", $output); ?>
Post Reply