如何在php中以某种格式显示数字 [英] how to display number in a certain format in php

查看:207
本文介绍了如何在php中以某种格式显示数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以一种统一的方式显示数字,整数或浮点数:

I'd like to display a number, integer or float in a uniform way like this:

如果它是整数,例如10,则应显示为10.00 如果它已经是一个浮点数,例如10.1,则应显示为10.10 如果浮点数看起来像这样的10.116,则只需在点后显示前两个数字 不管第三个数字后面的左数字.

if it is integer, for example 10, it should be displayed as 10.00 if it is a float already, for example 10.1, it should be displayed as 10.10 and if the float look like this 10.116, just display the first two number after the dot regardless the left digits following the third one.

推荐答案

$formatted = sprintf("%.2f", $your_number);

或者,如果您只想将其打印出来,而不需要在变量中使用它:

Alternatively, if you just want to print it out, and don't need it in a variable:

printf("%.2f", $your_number);

这篇关于如何在php中以某种格式显示数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆