Convert decimal number to Brazilian Portuguese standard

Leandro Lopes
Tera Contributor

I want to convert a decimal number in this format:

 

Ex 1.: 344310107052.78

Ex 2.: -71635021.7

 

to Brazilian Portuguese standard in this format in a Widget's Server Script:

 

Ex 1.: 344,310,107,052.78

Ex 2.: -71.635.021,7

 

I know that in Client Script we can use number.toLocaleString('pt-BR') which meets this requirement, but in Server Script this function does not work.

 

What function can I use in Server Script to perform this conversion, as per this requirement?

1 ACCEPTED SOLUTION

Hopefully someone can provide you a better solution but as a workaround you could look at using GlideCurrencyFormatter() to set the formatting.

GlideCurrencyFormatter | ServiceNow Developers

This seems to have formatting issues in my PDI with positive values, if you encounter same these may be resolvable with some crude manipulation.

 

var amount1 = '344310107052.78';
var currencyCode = 'BRL';
var formatString = '%v';
var exchangeValue1 = new sn_currency.GlideCurrencyFormatter(formatString);
exchangeValue1.setLocale("pt", "BR"); 
var myResult = exchangeValue1.format(amount1, currencyCode);
gs.info('amount1 OOB ' + myResult);
myResult = myResult.replaceAll('.', '#').replaceAll(',', '.').replaceAll('#', ',');
gs.info('amount1 Fixed ' + myResult);


var amount2 = '-71635021.7';
var currencyCode = 'BRL';
var formatString = '%v';
var exchangeValue2 = new sn_currency.GlideCurrencyFormatter(formatString);
exchangeValue2.setLocale("pt", "BR"); 
gs.info('amount2 ' + exchangeValue2.format(amount2, currencyCode));

 

View solution in original post

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, I would not expect there to be a server-side method to deliver this behavior as it is a client side 'display' function with the underpinning data 'converted' and stored server-side based on the fields data type IE a decimal number is a decimal number regardless of the way it is displayed to the end user.

Can you clarify your specific requirement\use case?

The requirement is an HTML that will be generated directly in Server Script, and converted to PDF, using ServiceNow's native PDF Generation API.

 

So, I need to format these numbers from the American standard to the Brazilian standard inside the Server Script

Hopefully someone can provide you a better solution but as a workaround you could look at using GlideCurrencyFormatter() to set the formatting.

GlideCurrencyFormatter | ServiceNow Developers

This seems to have formatting issues in my PDI with positive values, if you encounter same these may be resolvable with some crude manipulation.

 

var amount1 = '344310107052.78';
var currencyCode = 'BRL';
var formatString = '%v';
var exchangeValue1 = new sn_currency.GlideCurrencyFormatter(formatString);
exchangeValue1.setLocale("pt", "BR"); 
var myResult = exchangeValue1.format(amount1, currencyCode);
gs.info('amount1 OOB ' + myResult);
myResult = myResult.replaceAll('.', '#').replaceAll(',', '.').replaceAll('#', ',');
gs.info('amount1 Fixed ' + myResult);


var amount2 = '-71635021.7';
var currencyCode = 'BRL';
var formatString = '%v';
var exchangeValue2 = new sn_currency.GlideCurrencyFormatter(formatString);
exchangeValue2.setLocale("pt", "BR"); 
gs.info('amount2 ' + exchangeValue2.format(amount2, currencyCode));

 

Thanks @Tony Chatfield1!

 

Your code is absolutely correct.

 

I also created a solution:

 

/* Converte decimal no padrão USA para o BRL */
convertToBRL: function(decimalUSA) {
    var decimalFormatted = decimalUSA;
    decimalFormatted = decimalFormatted.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0];

    if (decimalFormatted.length >= 0 ) {
        var decimalFormattedAux = decimalFormatted.replace(/\./g,",");
        decimalFormatted = decimalFormattedAux.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
    }

    return decimalFormatted;
}

 

Thank you for your cooperation!!!