Js preserves the decimal point of the 4 effects of code sharing

  • 2020-03-30 02:39:26
  • OfStack

1. Set the decimal point to zero:
function returnFloat0(value) {
    value = Math.round(parseFloat(value));
    return value;
}

2. Reserve one decimal point:
function returnFloat1(value) {
    value = Math.round(parseFloat(value) * 10) / 10;
    if (value.toString().indexOf(".") < 0) {
        value = value.toString() + ".0";
    }
    return value;
}

3. Keep two decimal points
function returnFloat2(value){
    value = Math.round(parseFloat(value) * 100) / 100;
    if (value.toString().indexOf(".") < 0) {
        value = value.toString() + ".00";
    }
    return value;
}

4. Reserve two decimal points, and one decimal will automatically fill in zero
function returnFloat3(value) {
    value = Math.round(parseFloat(value) * 100) / 100;
    var xsd = value.toString().split(".");
    //Ext.log(xsd.length);
    if(xsd.length==1){
        value = value.toString()+".00";
        return value;
    }
    if(xsd.length>1){
        if(xsd[1].length<2){
            value = value.toString()+"0";
        }
        return value;
    }
}

Related articles: