UPDATED:
Using javascript or jQuery, how can I convert a number into it’s different variations:
eg:
1000000
to…
1,000,000 or 1000K
OR
1000
to…
1,000 or 1K
OR
1934 and 1234
to…
1,934 or -2K (under 2000 but over 1500)
or
1,234 or 1k+ (over 1000 but under 1500)
Can this is done in a function?
Hope this make sense.
C
You can add methods to
Number.prototype, so for example:Now you can call this as
var num = 1000; num.addCommas()and it will return"1,000". That’s just an example, but you’ll find that all the functions create will involve converting the numbers to strings early in the process then processing and returning the strings. (The separating integer and decimal part will probably be particularly useful so you might want to refactor that out into its own method.) Hopefully this is enough to get you started.Edit: Here’s how to do the K thing… this one’s a bit simpler:
Call this in the same way:
var num = 2000; num.k()