显示2-4个小数位(php)(showing between 2-4 decimal places (php))
我试图找出一种方法来产生以下输出:
4.0000 = 4.00
4.5000 = 4.50
4.454= 4.454
4.54545454 = 4.5455
4.450 = 4.45
基本上我想确保总是至少有2个小数位,如果有额外的零,则修剪为2位小数。 如果小数点后超过4位,则保持为4。
我可以用:
number_format($number, 4, '.', '');
然后rtrim删除零,但在许多情况下,这将留下少于2个小数位。
I am trying to figure out a way to produce the following output:
4.0000 = 4.00
4.5000 = 4.50
4.454= 4.454
4.54545454 = 4.5455
4.450 = 4.45
Basically I want to make sure there is ALWAYS at least 2 decimal places and if there are extra zeros, trim to 2 decimal places. If there is more than 4 decimal places round and keep at 4.
I can use:
number_format($number, 4, '.', '');
then rtrim to remove zeros, but this will leave me with less than 2 decimal places in many cases.
原文:https://stackoverflow.com/questions/20202472
更新时间:2020-01-11 03:54
最满意答案
function format_number($number) {
$str = number_format($number, 4, '.', '');
return preg_replace('/(?<=\d{2})0+$/', '', $str);
}
format_number(4.0) // "4.00"
format_number(4.5) // "4.50"
format_number(4.454) // "4.454"
format_number(4.54545454) // "4.5455"
format_number(4.450) // "4.45"
function format_number($number) {
$str = number_format($number, 4, '.', '');
return preg_replace('/(?<=\d{2})0+$/', '', $str);
}
format_number(4.0) // "4.00"
format_number(4.5) // "4.50"
format_number(4.454) // "4.454"
format_number(4.54545454) // "4.5455"
format_number(4.450) // "4.45"
2013-11-25
相关问答
$str = "1.23444";
print strlen(substr(strrchr($str, "."), 1));
$str = "1.23444";
print strlen(substr(strrchr($str, "."), 1));
你可以使用number_format() : return number_format((float)$number, 2, '.', '');
例: $foo = "105";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00
此函数返回一个字符串 。 You can use number_format(): return number_format((float)$number, 2, '.', '');
...
您可以使用正则表达式来解析value ,捕获小数位数并计算匹配的长度(如果有): import re
def num_decimal_places(value):
m = re.match(r"^[0-9]*\.([1-9]([0-9]*[1-9])?)0*$", value)
return len(m.group(1)) if m is not None else 0
这比使用多个if else分割字符串要少一些“原始”,但不确定是否更简单或更易读。 You can use
...
每个人都在围绕浮点数在内部表示中没有小数位数的事实而跳舞。 即浮点数100 == 100.0 == 100.00 == 100.000,并且全部由相同的数字表示,有效地为100并且以这种方式存储。 当数字表示为字符串时,此示例中的小数位数只有一个上下文。 在这种情况下,可以使用任何计算小数点后数字的字符串函数来检查。 Everybody is dancing around the fact that floating point numbers don't have a number of dec
...
function format_number($number) {
$str = number_format($number, 4, '.', '');
return preg_replace('/(?<=\d{2})0+$/', '', $str);
}
format_number(4.0) // "4.00"
format_number(4.5) // "4.50"
format_number(4.454) // "4.454"
form
...
对于舍入数字,你可以使用round() ,你也可以提到精度。 round($user['salary'], 2)
你可以在这里找到关于圆形函数的更多信息 希望这可以帮助。 For rounding numbers you can use the round() and you can mention the precision as well. round($user['salary'], 2)
You can find more information on round function H
...
您可以将0.0001添加到floatval($ startdate)并将0.0001添加到其他var以进行比较 floatval($startdate) + 0.0001 === $otherVar + 0.0001
You could add 0.0001 to floatval($startdate) and add 0.0001 to other var to compare them floatval($startdate) + 0.0001 === $otherVar + 0.0001
...
乘以100: $grandTotal = $grandTotal * 100; // (moves decimal 2 places to right)
或删除期间: $grandTotal = str_replace('.','',$grandTotal);
Either multiply by 100: $grandTotal = $grandTotal * 100; // (moves decimal 2 places to right)
or remove the periods:
...
对于这个问题,Javascript会更容易。 尝试这个:
function validateDecimal(sidd,decimalvalue) {
var sid = sidd;
var decimal = decimalvalue;
var txtdata = document.getElementById('txta_'+sid).value;
var s = txtdata.spli
...
在这里,你去伙计.. var total = (12.35 * 5) + (9.25 * 1) + (9.25 * 1) + (9.25 * 1)
$("#total").html(total.toFixed(2)); label{
border-bottom : 2px solid green;
font-weight: 900;
font-size: 32px;
}