0

I have a number, for example:

25297710.1088

I need to add a bit between them and leave two characters after the point. For example:

25 297 710.10

While I stopped at this:

$(td).text().reverse().replace(/((?:\d{2})\d)/g, '$1 ').reverse());
String.prototype.reverse = function() {
    return this.split('').reverse().join('');
}

From this code I get the following:

25 297 710.1 088

Where $(td).text() I get a number from the cell of the row in the table. If I have numbers, for example:

25297710.10

then i get:

25 297 710.10

It's ok.

What I need to do to leave two characters after the point?

Toto
  • 89,455
  • 62
  • 89
  • 125
mosxe
  • 1

2 Answers2

1

You can use a RegExp to format the number/string. The input is converted to string using the relevant toString method.

function formatNumber(input) {
  return input.toString().replace(/\d*(\d{2})(\d{3})(\d{3})\.(\d{2})\d*$/, "$1 $2 $3.$4");
}

var str = "25297710.1088";
var num1 = 25297710.1088;
var num2 = 2545454545454.2254;
var num3 = 232545454511112.3354122313123123;

console.log(formatNumber(str));
console.log(formatNumber(num1));
console.log(formatNumber(num2));
console.log(formatNumber(num3));
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • It's not works, because it does not leave two characters after the point and if we have numbers, for example:2545454545454.1054 - not works too. – mosxe Oct 20 '17 at 07:47
  • Fixed. Try again. – Ori Drori Oct 20 '17 at 08:23
  • Good. I update function on return input.toString().replace(/(\d{2})\d*$/, "$1").replace(/\B(?=(\d{3})+(?!\d))/g, " "); But when number: 22000 I got: 22 – mosxe Oct 20 '17 at 12:39
0

I think you can do next steps:

1) you have 25 297 710.10

2) you find position of dot symbol ->@pos

3) you replace bits in string in range between @pos and end of your string

4) you cut string after dot to 2 characters

Edd
  • 11
  • 1