tags:

views:

1485

answers:

5

Hi,

I'm doing a little application of adding prices and decimals. Points are normal to use with decimals, but how can I write decimal number with comma as input (543,35 instead of 543.35) and then maybe change it with point to the database (mysql)? Then print it back form the database with the comma. Reason is that comma (,) is more used in Finland than point (.) when write decimal numbers.

Thank you very much!

Samuel

+2  A: 

you need not do anything in the sql end. you want to format the decimal value in php (this assumes php4/php5): Set the third parameter $dec_point to ','

// string number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep )
<?php

$number = 1234.56;

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>

source: http://php.net/manual/en/function.number-format.php

Cheers!

mirezus
Thank you! Now can I introduce number with comma 12,50 instead of 12.50 (easier for finnish keyboard) so it could be recognice with the decimals?
sk
Right. You just need to worry about the formatting of the number on the front end and let MySQL store it as usual. The underlying number is still the same no matter how you display it.
mirezus
A: 

As it's been said, it's best to save the value as a float and then format for display. As an alternative to the previously suggested number_format(), you could try money_format() http://us.php.net/manual/en/function.money-format.php It doesn't work in a windows environment though if that is important to you.

kdevine
Whoops, I think I read your question too quickly... you don't need currency format, do you?
kdevine
Yes, no need for currency format. What I'm still looking for, is that is there any possibility to write decimal number with comma as input? I don't know if can formulate my question in right way?
sk
A: 

No, using comma as a decimal separator for arithmetic operations is not supported. Filter your input and replace the comma with a period, then format your output as you wish.

nikc
+1  A: 

the PHP number_format function is what you need :

number_format(5.50, 2, ',');

...should do the trick.

Nicolas
No it's not, read again.
Alix Axel
Ooops, it's actually not...my bad. (on a side not I'm always amazed to see people commenting on wrong answers without providing a correct one. Go on guys, you bring so much to the community :)
Nicolas
+2  A: 
$input  = '5,50';
$output = str_replace(',', '.', $input);
var_dump($output); // string(4) "5.50"
$number = (float)$output;
var_dump($number); // float(5.5)
Stefan Gehrig
Thank you! Wery good and clear answer!!
sk