1

I have a number is 1205000000, I want display at 1.205.000.000

number.toString().replace(/(\d{3})/g, "$1.").toString()

but result is 120.500.000.0 I don't want reverse a number.

QHu91_IT
  • 189
  • 2
  • 12

2 Answers2

0

One way would be to reverse the string before your manipulation and ther reverse it again. Like so:

var number = 1205000000;

function reverse(s) {
    return s.split("").reverse().join("");
}

var str = reverse(reverse(number.toString()).replace(/(\d{3})/g, "$1."));
alert(str);

See this working fiddle.

EDIT: See the comments. Its a bit dirty but for that specific number it will work. The link posted by @Artyom Neustroev as a comment under you question seems a whole lot better than this here.

Community
  • 1
  • 1
nozzleman
  • 9,529
  • 4
  • 37
  • 58
0

For the sake of correcting your regular expression (obviously for integer values only):

number.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1.');

www.regexper.com
ยป Detailed Regex Explanation

Emissary
  • 9,954
  • 8
  • 54
  • 65