1

I will immediately admit that I'm a java developer and are searching for a function similar to DecimalFormat, something like:

DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(Locale.getDefault());
unusualSymbols.setDecimalSeparator('|');
unusualSymbols.setGroupingSeparator('^');
DecimalFormat weirdFormatter = new DecimalFormat("#,##0.###", unusualSymbols);
Number myNum = weirdFormatter.parse("8^453|13");

Code adapted from Oracle javase i18n

Every resource I can find related to this is either just to format (not parsing)

How to specify thousand separator for number pipe in Angular 4

NativeScript: Formatting number typescript

or people using regex to substitute (that seems crazy to me)

How can I parse a string with a comma thousand separator to a number?

or some general topics that does not consider interationalization

TypeScript Converting a String to a number

In Typescript, How to check if a string is Numeric

I did some digging on angular's GitHub and I did find this class format_number but also this seems only related to formatting not parsing or at least I can not figure out how to use it.


The question is: Is there an object like this in Angular?, if not also this is a valid answer (I can stop searching). If you like to pass a solution using an external library please remember to add context around the link

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109

1 Answers1

1

I would simply do:

var numberStr = "8^453|13";
var number = parseFloat(numberStr.replace("^", "").replace("|", "."));
Adrian Theodorescu
  • 11,664
  • 2
  • 23
  • 30
  • Thanks for your answer, Yes I know how to do my simple example with regex (and I probably can figure out a more complex for all my cases) but to be honest my question is if angular have this class or not. The reason I'm looking for a non regex answer is that I would be surprised if there is not [better tools to do the job](https://softwareengineering.stackexchange.com/a/113243/227993) – Petter Friberg May 30 '18 at 16:22