Angular / Typescript numeral.js

I want to format the number to display the Romanian format for decimal places and thousands of them: 1.000,23. I am using the numeral.js library. Here is my pipe code:

    constructor(private session: SessionService) {
    numeral.register('locale', 'ro', {
        delimiters: {
            thousands: this.session.Settings.groupSymbols,
            decimal: this.session.Settings.decimalSymbols
        },
        abbreviations: {
            thousand: 'k',
            million: 'm',
            billion: 'b',
            trillion: 't'
        },
        ordinal: function (number) {
            return number === 1 ? 'er' : 'ème';
        },
        currency: {
            symbol: 'RON'
        }
    });
    numeral.locale("ro");
}

    public transform(value: string) {
    var b = numeral(value);
    var x = "0,0.";
     for (var i = 0; i < this.session.Settings.decimalDigits; i++) {
        x = x + "0";
    }

    return b.format(x);

Tag output is displayed:

   <label>{{ '12345678' | decimalUnitPipe }}</label>
  <br>
  <label>{{ '1.234,5678' | decimalUnitPipe }}</label>
  <br>
  <label>{{ '1,234.5678' | decimalUnitPipe }}</label>
  <br>
  <label>{{ '1,2345678' | decimalUnitPipe }}</label>
  <br>
  <label>{{ '1.2345678' | decimalUnitPipe }}</label>

And this is the output that the browser displays:

12,345,678.00 
1.23 
1,234.57 
12,345,678.00 
1.23

How can I convert my local file to display my numbers in the desired format, like in a shortcut. The decimal places (.00) are also displayed on the first number, and I do not want them to be displayed if I do not put any decimal places there. The second shows: 1.23, and I want it to show: 1.234.5678, etc.

Please help, thanks in advance!

+4
source share

Source: https://habr.com/ru/post/1683684/


All Articles