3

The value of the currency is larger than what javascript's numbers can hold, thus all computations must be done for string.

All the code that I came across with uses parseFloat() or parseInt() in some way.

Eg. The following string:
22222222222222222222.222222 is to be formatted as $22,222,222,222,222,222,222.22

EDIT: I cannot use third party libs. It is just for display at the moment, but a more generic approach to handling large numbers in js is appreciated :)

Arturo
  • 1,039
  • 1
  • 10
  • 21

3 Answers3

3

So long as the number you are using is a string then this should work.

var result = numberWithCommas("22222222222222222222.282222");

function numberWithCommas(x) {
    var parts = x.toString().split(".");
    parts[1] = parts[1].substr(0,2);
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");
}

JSFiddle: http://jsfiddle.net/markwylde/XNS6T/1/

this was based on the answer to a previous question provided by mikez302 in this topic

Community
  • 1
  • 1
Mark
  • 2,184
  • 1
  • 16
  • 28
  • 1
    thanks for the code Mark. I've modified this to add some validations, and implemented it. I have also added a rounding off for decimal instead of just `substr()`. Thanks :) – Arturo Aug 27 '13 at 14:21
  • I really like the regex, great example. – ars265 Aug 27 '13 at 23:11
1

I would suggest looking at a library to do this because with browser support among many other issues it can be a real pain. You might look at: http://josscrowcroft.github.io/accounting.js/ or you can see this answer How can I format numbers as money in JavaScript? Additionally, if you want to work with the large numbers yourself there are BigInt and other type libraries for that but many of them have bugs. It's easiest to do most of this logic server side for better accuracy.

Community
  • 1
  • 1
ars265
  • 1,949
  • 3
  • 21
  • 37
  • hi.. thanks for the link. actually I'm working on a 3rd party code, and thus can only use the libraries which they have provided. So, no external libs for me :( – Arturo Aug 27 '13 at 14:14
  • Indeed that does suck, I was doing the same with the BigInt stuff and actually made ajax requests to the server for accuracy, but if you're only dealing with formatting you should be fine. – ars265 Aug 27 '13 at 14:16
1

You could split the string and then rebuild it in your own format.

Start by spliting by the "." char, this will separate the decimals.

Then split by "" giving you an array with all the individual digits.

Now its a matter of finding out where to put the commas, you can calculate the modulus, like so:

var mod = digitArray.length % 3

This is essentially the offset of the first comma, the rest will be added every 3 digits, for instance, in a number with 13 digits the first comma will be after the first digit (13 % 3 = 1), example:

1234567890123 -> 1,234,567,890,123

Dont forget to append the decimals in the end.

cernunnos
  • 2,766
  • 1
  • 18
  • 18
  • yup i've written code following this algorithm (Mark Wyde's function below is also similar).. It'll do for now, but I want to write something more generic, that can allow me do calculations. But thanks for pointing me in the direction. – Arturo Aug 27 '13 at 14:19
  • A more generic approach would be more complex, but i imagine you could take advantage of the digit array, basically a [1,3,2,0] array is a representation of 1*10^3 + 3*10^2 + 2*10^1 + 0. – cernunnos Aug 27 '13 at 14:23