1
I have a pre-formatted value of 6.5
. If I put in this function, it returns 0,65
. If I put 19.5
he returns 1,95
.
Why does that happen?
function formataReal(numero)
{
var tmp = numero + '';
var neg = false;
if (tmp - (Math.round(numero)) == 0) {
tmp = tmp + '00';
}
if (tmp.indexOf(".")) {
tmp = tmp.replace(".", "");
}
if (tmp.indexOf("-") == 0) {
neg = true;
tmp = tmp.replace("-", "");
}
if (tmp.length == 1) tmp = "0" + tmp
tmp = tmp.replace(/([0-9]{2})$/g, ",$1");
if (tmp.length > 6)
tmp = tmp.replace(/([0-9]{3}),([0-9]{2}$)/g, ".$1,$2");
if (tmp.length > 9)
tmp = tmp.replace(/([0-9]{3}).([0-9]{3}),([0-9]{2}$)/g, ".$1.$2,$3");
if (tmp.length = 12)
tmp = tmp.replace(/([0-9]{3}).([0-9]{3}).([0-9]{3}),([0-9]{2}$)/g, ".$1.$2.$3,$4");
if (tmp.length > 12)
tmp = tmp.replace(/([0-9]{3}).([0-9]{3}).([0-9]{3}).([0-9]{3}),([0-9]{2}$)/g, ".$1.$2.$3.$4,$5");
if (tmp.indexOf(".") == 0) tmp = tmp.replace(".", "");
if (tmp.indexOf(",") == 0) tmp = tmp.replace(",", "0,");
return (neg ? '-' + tmp : tmp);
}
I improved the answer with example using locale of Brazil. I also simplified the text to be more "straight to the point".
– Zuul
No problem at all.
– DNick