1

Currently looking for an easy way to convert 99999.99 to 9.999,99 format:

This is what I have currently:

CREATE TEMP FUNCTION FORMAT_DE_VALUE(number FLOAT64)
RETURNS STRING
LANGUAGE js 
AS """
return number.toFixed(2) + ' €';
""";

TRY 1:

  • return number.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' });

  • NO Success - number stays the same

Oksana Ok
  • 515
  • 3
  • 7
  • 19
  • Possible duplicate of [Regular Expression for formatting numbers in JavaScript](https://stackoverflow.com/questions/2254185/regular-expression-for-formatting-numbers-in-javascript) – kemicofa ghost Oct 21 '19 at 13:38
  • `99999999999.99 .toFixed(2) .replace(".", ",") .replace(/(\d)(?=(\d{3})+(?!\d))/g, ".$1")` – kemicofa ghost Oct 21 '19 at 13:39
  • @kemicofa Regular Expression do not work with BigQuery - `Syntax error: Illegal escape sequence: \d at` – Oksana Ok Oct 21 '19 at 13:42

1 Answers1

1

If you can't use regex, then you can try something like this that uses QoL Array methods.

The idea is to start in reverse and to have each item of your list as a string containing a maximum of three digits.

const [whole, dec] = 9999999.99
.toFixed(2)
.split(".");

const res = whole
.split("")
.reverse()
.reduce((acc,cur)=>{
  if(acc[0].length === 3){
    acc.unshift("");
  }
  acc[0] = cur + acc[0];
  return acc;
}, [""])
.join(".")
.concat("," + dec)

console.log(res);
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131