用逗号代替点的十进制数(php和sql)

时间:2010-01-04 21:59:56

标签: php sql

我正在做一些添加价格和小数的应用程序。使用小数点是正常的,但是如何用逗号作为输入写入十进制数(543,35而不是543.35)然后可能用点指向数据库(mysql)来改变它?然后用逗号将数据打印回数据库。原因是,在写入十进制数时,逗号(,)在芬兰的使用比在点(。)中更多。

非常感谢!

塞缪尔

5 个答案:

答案 0 :(得分:10)

$input  = '5,50';
$output = str_replace(',', '.', $input);
var_dump($output); // string(4) "5.50"
$number = (float)$output;
var_dump($number); // float(5.5)

答案 1 :(得分:6)

你不需要在sql端做任何事情。你想格式化php中的十进制值(假设php4 / php5):将第三个参数$ dec_point设置为','

// 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

?>

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

干杯!

答案 2 :(得分:1)

PHP number_format功能是您所需要的:

number_format(5.50, 2, ',');

......应该这样做。

答案 3 :(得分:0)

正如所说,最好将值保存为浮点数然后格式化显示。作为之前建议的number_format()的替代方法,您可以尝试money_format()http://us.php.net/manual/en/function.money-format.php它在Windows环境中不起作用,但这对您来说很重要。

答案 4 :(得分:0)

不支持使用逗号作为算术运算的小数分隔符。过滤输入并用句点替换逗号,然后根据需要格式化输出。