Monetary unit with JSON

Asked

Viewed 359 times

2

I’m getting from the JSON a currency format as follows

"Preco" : "2299.0000"

It appears in my form (ASP Classic / HTML):

R$ 22.990.000,00

How to do this treatment? You have to use JS?

The correct value that should appear is: R$ 2299,00.

  • Do you have access to the code that generates this JSON? if I don’t think you really need to use JS, but then we need to see more examples of values. Might come some kind of value: "Preco" : "2299.0000,50" or always comes this way with 2 more house ?

  • I do. It comes from SQL Server, which generates an XML that passes to an API that sends me JSON values. I will have to do this formatting in Sqlserver?

  • The ideal is to have the right value in JSON, if you have access to the code that produces JSON. The best is [Dit] the question with that information and any related code.

  • I will do this treatment in the API that generates JSON then. Since I discovered another problem on the date (timestamp) Thanks for the advice.

3 answers

2


Hello,

You can try to format the number like this:

FormatNumber(12345.67899)

The exit will be:

12,345.68

More information here.

  • Even passing the direct number. How would I do if the value came from a Recordset? Ex: Formatnumber(rsGrid("price"),2)) .

  • Writing rsGrid("price") right on the page it comes from that way?

  • Come 2299.0000 ... What I didn’t understand. I put Sponse.write(rsGrid("price")) and this way comes. But when I put the formatNumber it comes 22,990,000.00

1

Hello now I usually use the two functions below in Javascript and work perfectly, I will post below and after I leave a few examples ok.

Javascript:

function moedaParaNumero(valor) {
    return isNaN(valor) == false ? parseFloat(valor) : parseFloat(valor.replace("R$", "").replace(".", "").replace(",", "."));

/* Exemplos:
* var a = moedaParaNumero("R$ 10,00"); 
* var b = moedaParaNumero("R$ 100"); 
* var c = moedaParaNumero("0,50"); 
* var d = moedaParaNumero("1.500,00"); 
* var e = moedaParaNumero("89");
* Resultado 10 - 100 - 0.5 - 1500 - 89    
*/
}

function numeroParaMoeda(n, c, d, t) {
    c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");

/* Exemplos:
* var a = numeroParaMoeda(10);
* var b = numeroParaMoeda(100);
* var c = numeroParaMoeda(0.5);
* var d = numeroParaMoeda(1500);
* var e = numeroParaMoeda(89);
* Resultado "10,00" - "100,00" - "0,50" - "1.500,00" - "89,00"
*/
}

In ASP Classic

<%
'Exemplo 01: FormatCurrency
response.write(FormatCurrency(20000))
'Resposta $20,000.00
'Referência http://www.w3schools.com/asp/func_formatcurrency.asp

'Exemplo 02: FormatNumber
response.write(FormatNumber(20000))
'Resposta 20,000.00
'Referência http://www.w3schools.com/asp/func_formatnumber.asp
%>

0

If you’re still wrong using the FormatNumber is because you may need to replace the semicolon first. Depending on the configuration of the IIS it considers the national standard for decimal separation.

resultado = replace(rsGrid("preco"),".",",")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.