Try this, I created a function insertDot()
, which takes as argument the number you want:
var a = 14013;
function insertDot(a){
a = a.toString(); // Transforma em String
var beforeDot = a.substring(0, a.length-2); // Captura do primeiro ao penúltimo caractere
var afterDot = a.substring(a.length-2, a.length); // Captura o penúltimo ao último caractere
return parseFloat(beforeDot + "." + afterDot); // retorna um NÚMERO com com o ponto inserido
}
document.body.innerHTML += insertDot(a);
document.body.innerHTML += "<br>";
document.body.innerHTML += insertDot(a) * 2;
So just do:
function insertDot(a){
a = a.toString();
return parseFloat(a.substring(0, a.length-2)+"."+a.substring(a.length-2, a.length));
}
for (var i in teste) {
for (var j in (teste2[teste[i]])) {
valorFinal[j] = resultado1 + resultado2;
valorFinal[j] = insertDot(valorFinal[j]);
}
}
With a second argument that gets the point position, as suggested by Sergio:
var a = 14013;
function insertDot(a, pos){
a = a.toString();
if(pos > 0){
a = parseFloat(a.substring(0, pos) + "." + a.substring(pos, a.length));
}else{
a = parseFloat(a.substring(0, a.length+pos) + "." + a.substring(a.length+pos, a.length));
}
return a;
}
document.body.innerHTML += insertDot(a, 1);
document.body.innerHTML += "<br>";
document.body.innerHTML += insertDot(a, -2);
document.body.innerHTML += "<br>";
document.body.innerHTML += insertDot(a, -2) * 2;
I liked it if I had a second argument with the point position, like
inserDot(192, -1)
then it was very flexible. But it seems that the AP is already satisfied with the other answer.– Sergio
@Sergio, I edited the answer only at the level of subsequent uses by other users. Thank you for the suggestion.
– Samir Braga
Thank you so much for the personal help, tested and also helped me!
– Leonardo Max