首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将数字格式化为只有两位小数?

问如何将数字格式化为只有两位小数?
EN

Stack Overflow用户
提问于 2010-01-03 02:43:16
回答 4查看 34.4K关注 0票数 13

我希望实数是例如12.92,但不是12.9241。有可能这样做吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-01-03 02:48:29

在PHP中,试试

代码语言:javascript
复制
$n = 1234.5678;

// Two decimal places, using '.' for the decimal separator
// and ',' for the thousands separator.
$formatted = number_format($n, 2, '.', ',');
// 1,234.57
票数 23
EN

Stack Overflow用户

发布于 2010-01-03 02:51:37

对于PHP你可以使用number_format(),对于MySQL你可以使用FORMAT()函数。

MySQL:http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_format

代码语言:javascript
复制
FORMAT(number, 2)

示例:

代码语言:javascript
复制
mysql> SELECT FORMAT(12332.123456, 4);
       -> '12,332.1235

PHP:http://php.net/manual/en/function.number-format.php

代码语言:javascript
复制
$number = 1234.5678;
$formatted_number = number_format($number, 2, '.', '');
// 1234.56
票数 4
EN

Stack Overflow用户

发布于 2018-10-03 02:32:47

代码语言:javascript
复制
$number = 1234.5678;
$teX = explode('.', $number);

if(isset($teX[1])){
    $de = substr($teX[1], 0, 2);
    $final = $teX[0].'.'.$de;
    $final = (float) $final;
}else{
    $final = $number;   
}

期末考试将是1234.56

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1992406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档