简体   繁体   English

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

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

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)? 使用小数点是正常的,但是如何用逗号作为输入写入十进制数(543,35而不是543.35)然后可能用点指向数据库(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 塞缪尔

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

you need not do anything in the sql end. 你不需要在SQL端做任何事情。 you want to format the decimal value in php (this assumes php4/php5): Set the third parameter $dec_point to ',' 你想格式化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

?>

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

Cheers! 干杯!

the PHP number_format function is what you need : 您需要PHP number_format函数:

number_format(5.50, 2, ',');

...should do the trick. ......应该做的伎俩。

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. 作为以前建议的number_format()的替代方法,您可以尝试money_format() http://us.php.net/manual/en/function.money-format.php它在Windows环境中不起作用,但如果对你很重要

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. 过滤输入并用句点替换逗号,然后根据需要格式化输出。

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

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