Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format numbers in JavaScript similar to C#

Is there a simple way to format numbers in JavaScript, similar to the formatting methods available in C# (or VB.NET) via ToString("format_provider") or String.Format()?

like image 953
xiemails Avatar asked Jul 01 '09 09:07

xiemails


People also ask

How do you format numbers in JavaScript?

JavaScript numbers can be formatted in different ways like commas, currency, etc. You can use the toFixed() method to format the number with decimal points, and the toLocaleString() method to format the number with commas and Intl. NumberFormat() method to format the number with currency.

What format are JavaScript numbers stored in?

Regular numbers in JavaScript are stored in 64-bit format IEEE-754, also known as “double precision floating point numbers”.

Is there a printf in JavaScript?

The equivalent of sprintf("%. 2f", num) in JavaScript seems to be num. toFixed(2) , which formats num to 2 decimal places, with rounding (but see @ars265's comment about Math. round below).

Is there .format in JavaScript?

In javascript, there is no built-in string formatting function.


1 Answers

Generally

  • Formatting numbers in JavaScript
  • Formatting numbers for currency display and more.

In jQuery

  • autoNumeric (a decent number formatter & input helper with locale support for jQuery 1.5+)
  • jQuery Format (a clientSide implementation of Java's SimpleDateFormat and NumberFormat)
  • jquery-numberformatter (number formatter with locale support)
like image 196
rahul Avatar answered Sep 29 '22 03:09

rahul