Problems to make number (Angular 5) accept decimals with comma

0

Hi, I have the following problem, I need that when assigning a variable of the number type to a number with decimal places with a comma (Ex 10,50) I do not stay in NaN, at the moment I only accept decimals with a point (Ex 10.50) but In my country, commas are used to separate decimals and points for thousands (eg 1,000,000.00).

Is there any way to make number use comma for decimals?

Thanks

    
asked by Guillermo 12.07.2018 в 16:17
source

1 answer

0

The solution could be to create a Pipe so you can use the variable with '. ' but visualize it with ', '. That way it would not be broken by using another format.

In the transform () of the pipe the variable is passed to string and returns with replace ('.', ',');

 import { Pipe, PipeTransform } from '@angular/core';

/**
 * Pipe name
 * @export
 * @class CurrencyCustom
 * @implements {PipeTransform}
 */
@Pipe({
    name: 'CurrencyCustom'
})
export class CurrencyCustom implements PipeTransform {
    /**
     * Convierte una cantidad separada por punto a coma.
     * @param {number} currency 9999.99
     * @returns {string} '9999,99'
     */
    transform(currency: number): string {
        return currency.toString().replace(/./g, ',');
    }
}
    
answered by 16.07.2018 в 09:20