Calculation of percentage javascript

Asked

Viewed 1,716 times

-1

I am developing a project and need to calculate the percentage of spread (profit on sale) example: (1 Eth): 107.178552387 which in case is $107.18 with a 5% spread (ie 5% profit on top of the total price on Eth) and soon after need to add 2.5% fee on top of the total amount (amount of Eth + % spread), how could I do this in javascript ? a below a "pseudo code" in javascript to illustrate better.

	$(function() {
		$('#calc_input').keyup(function() {
			var x = parseFloat($(this).attr('data-unitprice')),
			y = parseFloat($(this).val()),
			cot = y * x;
			$('#resucota').attr('value', cot);
		});
	});

	$(function() {
		$('#spread').keyup(function() {
			var z = parseFloat($(this).val());
			var a = parseFloat($('#resucota').val());
			var spread = z % a;
			$('#lucro').attr('value', spread);
			var total = 2.5% + spread
			$('#resucota').attr('value', total);
		});
	});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row justify-content-center" id="sone" style="">
				<div class="col-md-11 ">
					<div class="input-group input-group-lg">
						<div class="input-group-prepend">
							<span class="input-group-text unit unidade moedsele" style="text-transform: uppercase;" id="inputGroup-sizing-sm">eth</span>
						</div>
						<input id="calc_input" type="text" name="total1" class="form-control" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-lg" data-unitprice="107.178552387">
						<span class="input-group-text" id="inputGroup-sizing-lg">SPREAD</span>
						<input type="text" id="spread" class="form-control">
					</div>
					<hr>
					<div class="input-group input-group-lg">
						<div class="input-group-prepend">
							<span class="input-group-text" id="inputGroup-sizing-lg">USD</span>
						</div>
						<input readonly="" id="resucota" name="total" type="text" class="col-sm-6" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-lg">&nbsp;&nbsp;+ 2.5% taxa
					</div>

					<br>
					<div class="invertable-input">
						<div class="suporte">
							<div class="status_grupo">
								<div class="status_titulo m treecolun">Mínimo</div>
								<input id="min" disabled="" value="134.39278008" class="status_valor m treecolun">
								<div class="status_titulo m treecolun">USD</div>
							</div>
						</div>
					</div>
					<br><br>
					<div class="invertable-input">
						<div class="suporte">
							<div class="status_grupo">
								<div class="status_titulo m treecolun">Lucro</div>
								<input disabled="" id="cotacao" value="0" class="status_valor m treecolun">
								<div class="status_titulo m treecolun">USD</div>
							</div>
						</div>
					</div>
					<br><br>
					<div class="invertable-input">
						<div class="suporte">
							<div class="status_grupo">
								<div class="status_titulo m treecolun">Cotação</div>
								<input disabled="" id="cotacao" value="0" class="status_valor m treecolun">
								<div class="status_titulo m treecolun">USD</div>
							</div>
						</div>
					</div>
					<br><br>
				</div>
				<button type="submit" class="btn btn-info btn-fill btn-wd">GERAR ORDEM</button>
		</div>

1 answer

3


I will give an answer based on my interpretations:

You want to simply calculate the percentage, in this case, first a percentage value of a value, and then on top of that value (spread) get a new value. That is:

(107.18 + 5%) + 2.5%

Solution: "you can do this through basic mathematics, through multiplication, 100% equals *1, 1% equals *0.01. So you could do:

(107.18*1.05)*1.025

Browser other questions tagged

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