繁体   English   中英

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

[英]Decimal number with comma instead of point (php and sql)

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

非常感谢你!

塞缪尔

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

你不需要在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

干杯!

您需要PHP number_format函数:

number_format(5.50, 2, ',');

......应该做的伎俩。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM