PHP 将数字格式化为没有小数位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10332446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:56:25  来源:igfitidea点击:

PHP format a number to have no decimal places?

phpnumber-formatting

提问by EOB

I have a number (as string) like this: 1.00000

我有一个这样的数字(作为字符串):1.00000

How can I reformat such numbers to only look like 1?

如何将这些数字重新格式化为仅看起来像1

Thanks

谢谢

回答by John Conde

Use number_format()

number_format()

echo number_format('1.000000'); // prints 1

Or use intval()

或使用 intval()

echo intval('1.000000'); // prints 1

Or cast it as an integer

或将其转换为整数

echo (int) '1.000000'; // prints 1

回答by Jords

You can use the floor method for it to stay as a float

你可以使用 floor 方法让它保持浮动

floor(1.000000)