We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
要求:
使用了 toFixed() 函数来保留2位小数;
toFixed()
// 转换 Number 数据类型,保留2位小数 var fix2 = items[i].price; var num_price = new Number(fix2) ; var num_2 = num_price.toFixed(2);
当你使用 toFixed() 的时候,x.toFixed() 中 x 必须是 Number ;
x.toFixed()
x
Number
按道理来说是没问题的,结果是得到了 保留2位小数的数 , 但是当你累加的时候,会出现一个情况就是:
原因在于: toFixed() 是返回格式化的字符串
可以使用 Math.round() 函数
Math.round()
var num_price = new Number(fix2) ; var num_2 = Math.round(num_price * 100)/100;
这样就可以得到保留2位小数的值,并且是按照数学累加得到总值
The text was updated successfully, but these errors were encountered:
No branches or pull requests
具体详情如下:
要求:
错误的做法
当你使用
toFixed()
的时候,x.toFixed()
中x
必须是Number
;按道理来说是没问题的,结果是得到了 保留2位小数的数 , 但是当你累加的时候,会出现一个情况就是:
你累加的结果是字符串相加的情况。
如何解决?
这样就可以得到保留2位小数的值,并且是按照数学累加得到总值
The text was updated successfully, but these errors were encountered: