Take the rest of the decimal places and perform a conversion

Asked

Viewed 27 times

-1

I am very layman in javascript, but I think you can do this in this language. I tried it in a very trivial way, but I did not succeed. Basically I have a system that converts the amount into kg for boxes of cookies, for example:

function bisc_salg(){
	p3 = document.getElementById("multiplicando3").value;
	p4 = document.getElementById("multiplicador3").value;
	r = p3/p4;
	document.getElementById("resultado3").value = r.toFixed(3);
	}
<form id="bisc_salg" class="box_Branco">
<p class="desc_item"> Biscoito Salgado Cream Cracker </p>
  <input type="text" placeholder="Qtde." class="qtde_Req" id="multiplicando3" onChange="bisc_salg();"> 
  <input type="text" class="invisible" id="multiplicador3" readonly="readonly" value=4.8 onChange="bisc_salg();"> =
  <input type="text" class="retorno" id="resultado3" readonly="readonly"> caixas
</form>

Let’s assume that I have 14.8 kg and playing in this system I get: 3,083 which means 3 whole boxes and a packet of biscuits. That is, each package has 400 grams. How do I take this rest of the decimal place and convert into packages? More or less would have to return:

3 boxes and 1 package.

The summary data are: Each box has 12 packets of cookies and 4.8 Kg; each cookie has 400 grams.

  • I think this bill is wrong. If a package has 200g, apart from the extra package, it would be 19kg, that is 19,000 grams... dividing by 200g of each package, that’s 95 packages, so you can’t divide 95 into 3 boxes.

  • I’m sorry, I’ve already made the correction.

1 answer

1


You can use the module operator % that returns the rest of the division...

9 % 3 // retorna 0
10 % 3 // retorna 1
11 % 3 // retorna 2
12 % 3 // retorna 0

So if you divide the result of the module by the weight of the package (0.4kg) you will have the amount of packages...

console.log('pacotes:', Math.round(14.8 % 4.8 / 0.4));
console.log('pacotes:', Math.round(15.2 % 4.8 / 0.4));

The Math.round is because of the error of splitting with floating points... See more here

Browser other questions tagged

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