resultados[i].value = (((num1 * num2).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ","));
Example:
function calcular(){
var valores1 = document.getElementsByClassName('txt1');
var valores2 = document.getElementsByClassName('txt2');
var resultados = document.getElementsByClassName('result');
for (let i = 0; i < valores1.length; ++i){
let num1 = parseFloat(valores1[i].value);
let num2 = parseFloat(valores2[i].value);
//resultados[i].value = (num1 * num2).toFixed(2);
resultados[i].value = (((num1 * num2).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ","));
}
}
<input class="txt1" value="80.50" name="opcao1" id="opcao1" type="text">
<input class="txt2" value="52" name="opcao2" id="opcao2" type="text">
<input class="result" value="" name="opcao3" id="opcao3" type="text" onclick="calcular()" placeholder="Clique aqui para calcular">
The idea is to match recursively - with the flag g
(global) - doing a positive Lookahead (?=(\d{3})+(?!\d))
- a sequence of 3 digits (\d{3})
as long as there is no digit on the right (?!\d)
of this sequence - and that it is not start or end of the chain \B
Lookahead is a way of marrying strings that have a certain ending or not. It is used (?=...) for the positive, that is, ending with, and (?!...) for the negative, that is, it does not end with the negative.
A simple example would be the search for Rafael followed by Ferreira. If there was Rafael or Rafael Outracoisa, he would not marry. /Rafael(?= Ferreira)/
On the contrary, in this example, I would only marry Rafael or Rafael Outracoisa, but I would not marry Rafael Ferreira: /Rafael(?! Ferreira)
Have you researched here on [pt.so]? What did you think?
– Woss
the link https://answall.com/questions/55085/como-formatr-um-valor-em-javascript may be useful to you.
– Leonardo Paim
The result of your question is wrong. rs
– Bsalvo
80.50 * 52 = 4,186.00
– user60252
Yes, I typed wrong hahaha.
– Rafael Ferreira
I don’t think you’ve been to math class
– user60252
I used the calculator to calculate, for calculating 80.50 * 52 head I would never calculate.
– Rafael Ferreira
then.on that day that you missed, the teacher taught to use the calculation machine kkkk It is always good to choose an answer as accepted, see why here https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-accepta reply/1079#1079
– user60252