0

How would I convert something like 1200000 to £1.2m but also convert 2675000 to £2.675m

i can get the second one to work but the first one comes out as £12m rather than £1.2m

I have the number in a variable so.

salePrice.toString().replace(/0+$/g, '').replace(/\B(?=(\d{3})+(?!\d))/g, '.')}m

how would i change the second replace to work as I guess it is that one that is causing the issue.

as long as this passes

1200000 1220000 1222000 1222200 1222220 1222222 1020000 1022200

so on and so forth all of them need to be able to pass.

Andy Wilson
  • 121
  • 8

2 Answers2

1

You have Number.prototype.toFixed() option available.

  const data = [
    2675000,
    1200000,
    1220000,
    1222000,
    1222200,
    1222220,
    1222222,
    1020000,
    1022200
  ];

const formatted = data.map(x=> (x/1000000).toFixed(3).replace(/0+$/g, '')); // ["2.675", "1.2", "1.22", "1.222", "1.222", "1.222", "1.222", "1.02", "1.022"]

I haven't included the part with the currency, because you had that figured out already. Shilly's answer is really beautiful. I'm simply proposing another solution, which is a bit shorter.

nemanja
  • 664
  • 5
  • 16
  • Your answer is very pretty but when i replace my .toString with .toFixed(3) i get £1.200.000.m rather than £1.2m, my function now looks like `salePrice.toFixed(3).replace(/0+$/g, '').replace(/\B(?=(\d{3})+(?!\d))/g, '.')}m` edit: my bad i missed the divide – Andy Wilson Nov 30 '18 at 11:25
  • But why would you do that? I have given you the solution which will output the array of formatted values. Hint: check `const formatted`. This solution wasn't meant as addition, or to be mixed with your regex. It works standalone. – nemanja Nov 30 '18 at 11:27
  • My question would be, why would you use toFixed(3) AND then remove all the extra zeroes, when just dividing will already remove the zeroes and using toFixed() adds these back? So you can rewrite this as `data.map(x=> x/1000000)`, which is basically my answer as well. I just included the extras to show how you'd deal with multiple currencies without the need to rewrite the formatting code. But +1, thanks. – Shilly Nov 30 '18 at 12:28
  • @Shilly Because `1222200` will output to `1.2222` and the way I understood the question is that OP wants it to be rounded to max 3 decimal places (with zeroes deleted). – nemanja Nov 30 '18 at 12:34
  • Ah I see the interpretation. I was fiddling with toFixed() and toPrecision() as well, but then noticed that the variable was called salePrice, so dropping any significant number would probably cause consumer complaints. – Shilly Nov 30 '18 at 12:37
  • @NemanjaGlumac this doesn't work with 120000000 it displays 120.m – Andy Wilson Dec 03 '18 at 07:59
  • @AndyWilson and it will not work with billions or tens or hundreds of thousands. You have asked specifically for millions and I have proposed a solution for that case. The point of StackOverflow is to help someone learn something new, and nudge them, give them a hint, if you will... So, try to understand how and why the answer works, modify it to suit your needs. – nemanja Dec 03 '18 at 10:41
0

You divide them by the precision you need. I would advice to keep numbers as numbers as long as possible, since strings used as numbers follow text rules instead of math rules, so you'd have to parse them back to numbers to do anything meaningful apart from formatting the output.

const data = [
  2675000,
  1200000,
  1220000,
  1222000,
  1222200,
  1222220,
  1222222,
  1020000,
  1022200
];

const format_currency = ( prefix, value, precision, suffix ) => `${ prefix }${ value / precision }${ suffix }`;

const million = {
  symbol: 'm',
  value: 1000000
};

const pounds = '£';

const results = data.map( entry => format_currency( pounds, entry, million.value, million.symbol ));

console.log( results );
Shilly
  • 8,511
  • 1
  • 18
  • 24