[英]How to parse float with two decimal places in javascript?
我有以下代码。 我想要这样,如果 price_result 等于 integer,比如说 10,那么我想添加两位小数。 所以 10 就是 10.00。 或者如果它等于 10.6 将是 10.60。 不知道该怎么做。
price_result = parseFloat(test_var.split('$')[1].slice(0,-1));
您可以使用toFixed()来做到这一点
var twoPlacedFloat = parseFloat(yourString).toFixed(2)
如果您需要性能(例如在游戏中):
Math.round(number * 100) / 100
它大约是 parseFloat(number.toFixed(2)) 的 100 倍
当您使用toFixed
,它总是以字符串形式返回值。 这有时会使代码复杂化。 为避免这种情况,您可以为 Number 制作另一种方法。
Number.prototype.round = function(p) {
p = p || 10;
return parseFloat( this.toFixed(p) );
};
并使用:
var n = 22 / 7; // 3.142857142857143
n.round(3); // 3.143
或者干脆:
(22/7).round(3); // 3.143
要返回数字,请添加另一层括号。 保持清洁。
var twoPlacedFloat = parseFloat((10.02745).toFixed(2));
如果您的目标是解析,并且您的输入可能是文字,那么您会期望float
和toFixed
不会提供,所以这里有两个简单的函数来提供这个:
function parseFloat2Decimals(value) {
return parseFloat(parseFloat(value).toFixed(2));
}
function parseFloat2Decimals(value,decimalPlaces) {
return parseFloat(parseFloat(value).toFixed(decimalPlaces));
}
来自 lodash 的 ceil 可能是最好的
_.ceil("315.9250488",2)
_.ceil(315.9250488,2)
_.ceil(undefined,2)
_.ceil(null,2)
_.ceil("",2)
也可以使用数字,而且很安全
如果您不想四舍五入,请使用以下功能。
function ConvertToDecimal(num) {
num = num.toString(); //If it's not already a String
num = num.slice(0, (num.indexOf(".")) + 3); //With 3 exposing the hundredths place
alert('M : ' + Number(num)); //If you need it back as a Number
}
对于它的价值:十进制数是十进制数,您可以将其四舍五入为其他值或不四舍五入。 在内部,它会根据浮点运算和处理规则来近似一个小数部分。 它在内部保持十进制数(浮点数,在 JS 中为双精度数),无论您想用多少位数显示它。
要将其呈现以供显示,您可以通过字符串转换将显示精度选择为您想要的任何值。 演示是显示问题,而不是存储问题。
您可以使用 .toFixed() 来获取浮点值 2 位
例子
let newValue = parseFloat(9.990000).toFixed(2) //output 9.99
我已经为我的案例尝试过这个,它会正常工作。
var multiplied_value = parseFloat(given_quantity*given_price).toFixed(3);
示例输出:
9.007
您可以将价格存储为字符串
您可以使用 Number(string)
为您的计算。
例子
Number("34.50") == 34.5
还有
Number("35.65") == 35.65
如果您对 Number 函数感到满意,则可以使用它。
parseFloat(parseFloat(amount).toFixed(2))
你必须解析它两次。 第一次是将字符串转换为浮点数,然后将其固定为两位小数(但toFixed
返回的是字符串),最后再次解析。
简单的 JavaScript,要浮动的字符串:
var it_price = chief_double($("#ContentPlaceHolder1_txt_it_price").val());
function chief_double(num){
var n = parseFloat(num);
if (isNaN(n)) {
return "0";
}
else {
return parseFloat(num);
}
}
试试这个(见代码注释):
function fixInteger(el) {
// this is element's value selector, you should use your own
value = $(el).val();
if (value == '') {
value = 0;
}
newValue = parseInt(value);
// if new value is Nan (when input is a string with no integers in it)
if (isNaN(newValue)) {
value = 0;
newValue = parseInt(value);
}
// apply new value to element
$(el).val(newValue);
}
function fixPrice(el) {
// this is element's value selector, you should use your own
value = $(el).val();
if (value == '') {
value = 0;
}
newValue = parseFloat(value.replace(',', '.')).toFixed(2);
// if new value is Nan (when input is a string with no integers in it)
if (isNaN(newValue)) {
value = 0;
newValue = parseFloat(value).toFixed(2);
}
// apply new value to element
$(el).val(newValue);
}
Solution for FormArray controllers
初始化 FormArray 表单生成器
formInitilize() {
this.Form = this._formBuilder.group({
formArray: this._formBuilder.array([this.createForm()])
});
}
创建表格
createForm() {
return (this.Form = this._formBuilder.group({
convertodecimal: ['']
}));
}
将表单值设置到表单控制器中
setFormvalues() {
this.Form.setControl('formArray', this._formBuilder.array([]));
const control = <FormArray>this.resourceBalanceForm.controls['formArray'];
this.ListArrayValues.forEach((x) => {
control.push(this.buildForm(x));
});
}
private buildForm(x): FormGroup {
const bindvalues= this._formBuilder.group({
convertodecimal: x.ArrayCollection1? parseFloat(x.ArrayCollection1[0].name).toFixed(2) : '' // Option for array collection
// convertodecimal: x.number.toFixed(2) --- option for two decimal value
});
return bindvalues;
}
我有其他解决方案。
您可以使用round()
来代替toFixed()
var twoPlacedFloat = parseFloat(yourString).round(2)
对我有用的解决方案如下
parseFloat(value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.