Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数字格式PHP欧洲格式(=3.213.1244355),不带0';十进位_Php - Fatal编程技术网

数字格式PHP欧洲格式(=3.213.1244355),不带0';十进位

数字格式PHP欧洲格式(=3.213.1244355),不带0';十进位,php,Php,我想在PHP中按如下方式转换数字: 100000 -> 100.000 3213 -> 3.213 54523.321 -> 54.523,321 42324.00 -> 42.324 3412.1 -> 3.412,1 所以我想要一个新的。作为千位分隔符和小数点分隔符,我不希望在小数点中使用更少的0。我该怎么做 我知道我可以用float来去掉小数点后的0。我知道我可以使用数字\格式来替换千位/小数分隔符,但在这种情况下,您还必须定义小数的数量,您将得到小数中的0

我想在PHP中按如下方式转换数字:

100000 -> 100.000
3213 -> 3.213
54523.321 -> 54.523,321
42324.00 -> 42.324
3412.1 -> 3.412,1
所以我想要一个新的。作为千位分隔符和小数点分隔符,我不希望在小数点中使用更少的0。我该怎么做


我知道我可以用float来去掉小数点后的0。我知道我可以使用数字\格式来替换千位/小数分隔符,但在这种情况下,您还必须定义小数的数量,您将得到小数中的0…

使用以下方法:

$number = 1234.5600;
$nb = number_format($number, 2, ',', '.'); // 1.234,56
它将自动删除小数末尾的所有零。

自己找到答案(使用/更改我在评论中找到的代码):

这里还有一个包含舍入的函数,但不添加无用的0:(我认为正常的number_format()函数应该是这样工作的…)


“我知道我可以用float来去掉小数点后的0”-不,你可以转换成整数或使用round()函数。“但在这种情况下,你还必须定义小数点的数量,你将得到小数点后的0”-然后用空字符串替换结果中的尾随
,00
。?“欧洲格式”-哪种欧洲格式?有几个。服务器和客户端都有一些关于如何显示不同位置的数字(以及日期和其他内容)的详细信息,但访问这些数据的方式因服务器操作系统和其他考虑因素而异。我是提供文档链接的贴纸,因此,我编辑了你的答案。但是如果$number是1234.00,这将给出1.234,00而不是1.234。。。。。这就是我的问题!:)@Robber不要只使用then
floatval(数字格式($number,2',',',','))
和voilá:-)谢谢,但这也将为您的1234.5600美元提供1.234。。。因此,我正在寻找一个函数,它将删除十进制中的所有0,并使用右分隔符。或者,这在标准PHP函数中是不可能的?此外,对于$number=12345600,floatval也给出了奇怪的结果;(12.345而不是12.345.600)
function number_format_unchanged_precision($number, $dec_point='.', $thousands_sep=','){
    if($dec_point==$thousands_sep){
        trigger_error('2 parameters for ' . __METHOD__ . '() have the same value, that is "' . $dec_point . '" for $dec_point and $thousands_sep', E_USER_WARNING);
        // It corresponds "PHP Warning:  Wrong parameter count for number_format()", which occurs when you use $dec_point without $thousands_sep to number_format().
    }
    $decimals = strlen(substr(strrchr($number, "."), 1));

    return number_format($number, $decimals, $dec_point, $thousands_sep);
}
function number_format_without_zeroindecimal($number, $maxdecimal, $dec_point='.', $thousands_sep=','){
    if($dec_point==$thousands_sep){
        trigger_error('2 parameters for ' . __METHOD__ . '() have the same value, that is "' . $dec_point . '" for $dec_point and $thousands_sep', E_USER_WARNING);
        // It corresponds "PHP Warning:  Wrong parameter count for number_format()", which occurs when you use $dec_point without $thousands_sep to number_format().
    }
    $decimals = strlen(substr(strrchr(round($number,$maxdecimal), "."), 1));

    return number_format($number, $decimals, $dec_point, $thousands_sep);
}