JavaScript Number Format – Decimal Precision

來源:互聯網
上載者:User

Introduction

JavaScript has built-in methods to format a number to a certain precision. They are toFixed and toPrecision, and are part of the Number object. Any browser that supports ECMAScript version 3 should support toFixed and toPrecision. This roughly equates to Netscape 6.0 and above and IE 5.5 and above.

Examples

Use toFixed to set precision after the decimal point. It doesn't matter how large the number is before the decimal point. For normal decimal formatting, this is your best option.

// Example: toFixed(2) when the number has no decimal places// It will add trailing zerosvar num = 10;var result = num.toFixed(2); // result will equal 10.00// Example: toFixed(3) when the number has decimal places// It will round to the thousandths placenum = 930.9805;result = num.toFixed(3); // result will equal 930.981
Try toFixed

 

 

Use toPrecision when you're setting the overall precision. Here, it matters how large the number is before and after the decimal point. This is more useful for mathematical purposes than for formatting.

// Example: toPrecision(4) when the number has 7 digits (3 before, 4 after)// It will round to the tenths placenum = 500.2349;result = num.toPrecision(4); // result will equal 500.2// Example: toPrecision(4) when the number has 8 digits (4 before, 4 after)// It will round to the ones placenum = 5000.2349;result = num.toPrecision(4); // result will equal 5000// Example: toPrecision(2) when the number has 5 digits (3 before, 2 after)// It will round to the tens place expressed as an exponentialnum = 555.55;result = num.toPrecision(2); // result will equal 5.6e+2
Floating-point errors

toFixed and toPrecision are subject to floating-point errors.

Here is a test where the starting number is 162.295. The following should show the JavaScript results:

162.29 // toFixed(2)
162.29 // toPrecision(5)

Do they show up correctly as 162.30 in your browser? Most JavaScript implementations will display it as 162.29

Here is basically what happens when rounding 162.295 to two decimal places

num = 162.295
num *= 100 // 16229.499999999998
num = Math.round(num) // 16229
num /= 100 // 162.29

As you can tell, it's in the second step that the number changes from its actual value.

Floating-point numbers - External references

bugnet.com - JavaScript Math Errors in Netscape & Internet Explorer 
wikipedia.org - Problems with floating-point

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.