最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to get a float using parseFloat(0.00) - Stack Overflow

programmeradmin2浏览0评论

How can I get a float of 0.00. The reason I need 0.00, is because I am going to be accumalating float values by adding. Hence starting with 0.00. I tried

 var tmp ='0.00'
 tmp  = parseFloat(tmp.toString()).toFixed(2);
 totals = parseFloat(tmp)

tmp is 0.00 but totals is 0. How can I make total 0.00? I need it to stay as a float and not a string. Thanks

How can I get a float of 0.00. The reason I need 0.00, is because I am going to be accumalating float values by adding. Hence starting with 0.00. I tried

 var tmp ='0.00'
 tmp  = parseFloat(tmp.toString()).toFixed(2);
 totals = parseFloat(tmp)

tmp is 0.00 but totals is 0. How can I make total 0.00? I need it to stay as a float and not a string. Thanks

Share Improve this question asked Jun 24, 2014 at 19:12 MaryMary 1,5956 gold badges30 silver badges47 bronze badges 2
  • 2 JS doesn't actually have 'float' or 'int' types. They are all of type Number. It just happens to strip off the .00 when you're displaying it. See Be careful with JS numbers! – p.s.w.g Commented Jun 24, 2014 at 19:17
  • 1 That makes no sense at all – real numbers do not have just zeros behind a decimal point ever. Displaying a value without any decimals as .00 is purely a matter of formatting. – C3roe Commented Jun 24, 2014 at 19:18
Add a comment  | 

5 Answers 5

Reset to default 5

You can use the string tmp variable and then when you need to add to it use:

tmp = (+tmp + 8).toFixed(2);

JSFIDDLE DEMO

Or simply write a function to do that seeing that you'll have to do that many times:

function strAdd( tmp, num ) {
    return (+tmp + num).toFixed(2);
}

There is no such thing as an integer type in javascript. There is only Number, which is stored as a double precision floating point. So to get a floating point value with 0.00, you need only to do this:

var tmp = 0;
var tmp ='0.00'
tmp  = parseFloat(tmp.toString()).toFixed(2);
totals=parseFloat(tmp).toFixed(2);
alert(totals); //0.00

parseFloat() without toFixed() removes zeros after dot. So you need to add toFixed() again.

Here is dynamic floatParser for those who need

function customParseFloat(number){
  if(isNaN(parseFloat(number)) === false){
    let toFixedLength = 0;
    let str = String(number);

    // You may add/remove seperator according to your needs
    [".", ","].forEach(seperator=>{
      let arr = str.split(seperator);
      if( arr.length === 2 ){
        toFixedLength = arr[1].length;
      }
    })

    return parseFloat(str).toFixed(toFixedLength);
  }

  return number; // Not a number, so you may throw exception or return number itself
}

You can use parseFloat function in Javascript.

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var a = parseFloat("10") + "<br>";
    var b = parseFloat("10.00") + "<br>";
    var c = parseFloat("10.33") + "<br>";
    var d = parseFloat("34 45 66") + "<br>";
    var e = parseFloat("   60   ") + "<br>";
    var f = parseFloat("40 years") + "<br>";
    var g = parseFloat("He was 40") + "<br>";

    var n = a + b + c + d + e + f + g;
    document.getElementById("demo").innerHTML = n;
}
</script>
发布评论

评论列表(0)

  1. 暂无评论