我有下面的代码。我想让它这样,如果price_result等于一个整数,比如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)

我有其他的解。

你可以使用round()来代替toFixed()

var twoPlacedFloat = parseFloat(yourString).round(2)

当你使用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

试试这个(参见代码中的注释):

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);
}

要返回一个数字,添加另一层圆括号。保持清洁。

var twoPlacedFloat = parseFloat((10.02745).toFixed(2));

如果你需要性能(游戏邦注:比如在游戏中):

Math.round(number * 100) / 100

它大约是parseFloat(number.toFixed(2))速度的100倍。

http://jsperf.com/parsefloat-tofixed-vs-math-round


如果你不想四舍五入,请使用下面的函数。

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中是double),无论你想用多少位数字来显示它。

要将其表示为显示,可以通过字符串转换选择显示的精度。表示是一个显示问题,而不是存储问题。


如果你的目标是解析,你的输入可能是一个文字,那么你会期望一个浮点数,而toFixed不会提供这个,所以这里有两个简单的函数来提供这个:

function parseFloat2Decimals(value) {
    return parseFloat(parseFloat(value).toFixed(2));
}

function parseFloat2Decimals(value,decimalPlaces) {
    return parseFloat(parseFloat(value).toFixed(decimalPlaces));
}

来自洛达什的西尔可能是最好的

_.ceil("315.9250488",2) 
_.ceil(315.9250488,2) 
_.ceil(undefined,2)
_.ceil(null,2)
_.ceil("",2)

也会工作与一个数字,它是安全的


下面是对我有效的解决方案

parseFloat(value)

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;
  }

@sd 简单的回答:在JS中没有办法有数字数据类型的值与小数后面的零。

长回答:它是JavaScript的属性toFixed或toPrecision函数,用于返回String。这样做的原因是数字数据类型不能有像a = 2.00这样的值,它总是会删除小数后面的零,这是数字数据类型的内置属性。因此,要在JS中实现上述目标,我们有两个选择

要么使用数据作为字符串,要么 同意在ex2.50 -> 2.5结尾使用大小写为“0”的截断值。


您可以使用.toFixed()来表示浮点值2位

Exampale

let newValue = parseFloat(9.990000).toFixed(2) / /输出 9.99


我已经为我的案子试过了,它会工作得很好。

var multiplied_value = parseFloat(given_quantity*given_price).toFixed(3);

样例输出:

9.007


您可以将价格存储为字符串

你可以使用 (字符串)

用于计算。

例子

Number("34.50") == 34.5

also

Number("35.65") == 35.65

如果你对数字函数感到舒服,你可以使用它。


parseFloat(parseFloat(amount).toFixed(2))

你必须解析两次。第一次是将字符串转换为浮点数,然后将其固定为两个小数(但toFixed返回一个字符串),最后再次解析它。