This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
How to round float numbers in javascript?
(19 answers)
Closed 9 years ago.
I am working on a calculator and would like it to round to about 5 decimal places. Here is my javascript code to get an inout and produce the output:
<div class="input">
Price paid per share: <input id="value1" type="text" /><br>
Number of shares bought: <input id="value2" type="text" /><br>
Commission/ fee's paid: <input id="value3" type="text" /><br>
<center><input type="submit" class="submit" onclick="output();"></center>
</div>
<center><p class="result" id="result"> </p></center>
<script type="text/javascript" language="javascript" charset="utf-8">
function output(){
var value1 = document.getElementById('value1').value;
var value2 = document.getElementById('value2').value;
var value3 = document.getElementById('value3').value;
document.getElementById('result').innerHTML = ((parseFloat(value1) * parseFloat(value2)) + (parseFloat(value3))) / (parseFloat(value2));
}
Anyone know how I could round my output/ results to about 5 decimal places? Thanks.
var num=4.5960797;
var n=num.toFixed(5);
Try using toFixed:
var result = ((parseFloat(value1) * parseFloat(value2)) + (parseFloat(value3))) / (parseFloat(value2));
document.getElementById('result').innerHTML = result.toFixed(5);
Use Math.round(num * 100000) / 100000
var result=Math.round(yourValue*100000)/100000
Related
This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Do DOM tree elements with IDs become global properties?
(5 answers)
Closed 4 months ago.
When I tried putting in values any value that is an integer it will just print out [object HTMLinputElement] x 1 = NaN. I have also tried changing the .innerHTML to .innerText and .value but it still result in the same thing. This result in the multiplication operation not executing.
function calculate() {
for (var i = 1; i <= 12; i++) {
document.getElementById('output').innerHTML += (tbInteger + " x " + i + " = " + tbInteger * i + '<br>')
}
}
<h1>Math Drills</h1>
<!-- Math Operator Input -->
Select an arithmetic operation (* or +):
<input type="text" id="tbOperator" value="*">
<br> <br>
<!-- Integer Input -->
Enter a positive integer value between 1 and 100:
<input type="number" id="tbInteger">
<br> <br>
<!-- Buttons -->
<input type="button" value="Calculate" class="button" onclick="calculate();" />
<input type="button" value="Clear" class="button" onclick="" />
<!-- Output -->
<div id="output"></div>
This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
How to force addition instead of concatenation in javascript [duplicate]
(3 answers)
Closed 3 years ago.
Here are my code and its showing 00.000.000.00. I have changed this code another way then it's showing only the first digit of the result.
$(document).on('change', '.prc', function() {
var tSum = 0;
$('.prc').each(function() {
tSum += parseFloat($(this).val());
});
$('.totalprc').val(tSum);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="prc" value="0.00" />
<input class="prc" value="0.00" />
<input class="prc" value="0.00" />
<input class="totalprc" />
This question already has answers here:
How to convert a string of numbers to an array of numbers?
(18 answers)
How to sort an array of integers correctly
(32 answers)
Closed 4 years ago.
I'm trying to resolve this course but as a beginner in JavaScript, it's a little hard. this course is about sorting an array of numbers (sorry for my bad english it's not my first language )
i used the method .sort() cause the numbers are in an array but it keep telling me that it's not a fonction..
numbers = document.getElementById("numbers").value;
(function() { document.getElementById("run").addEventListener("click", function() {
numbers.sort()
alert(numbers);
});
})();
<div class="field">
<label for="numbers">Numbers:</label>
<input type="text" name="numbers" id="numbers" readonly value="2, 4, 14, 10, 90, 23, 16" />
</div>
<div class="actions">
<button type="button" id="run">Run</button>
</div>
when i run my code it says that numbers.sort() is not a fonction
but how can i use this method ??
Thanks you in advance !
numbers is a string so you can't use sort on string directly. you need to change it to array. Here i am doing it using split function.
function handle() {
let numbers = document.getElementById("numbers").value;
numbers=numbers.split(',').sort((a,b)=>a-b)
alert(numbers);
};
<div class="field">
<label for="numbers">Numbers:</label>
<input type="text" name="numbers" id="numbers" readonly value="2,4,14,10,90,23,16" />
</div>
<div class="actions">
<button type="button" id="run" onclick='handle()'>Run</button>
</div>
the input value is just a a long string.
You need to split the string into an array of string representation of numbers, then convert them to number type.
to use the compare function, you need to to provide a compare function, otherwise, the function sort by the unicode representation.
Here is a working code:
function handle() {
var numbersInputElemnt = document.querySelector('#numbers');
var numbersStr = numbersInputElemnt.value;
var numbers = numbersStr.split(', ').map(function(numStr) {
return +numStr;
});
numbers.sort(function compareNumbers(a, b) {
return a - b;
});
alert(numbers);
};
<div class="field">
<label for="numbers">Numbers:</label>
<input type="text" name="numbers" id="numbers" readonly value="2, 4, 14, 10, 90, 23, 16" />
</div>
<div class="actions">
<button type="button" id="run" onclick="handle()">Run</button>
</div>
This question already has answers here:
Substracting values on keyup using jquery
(3 answers)
Closed 9 years ago.
I have two textbox as follow:
<input id="amount" name="amount" type="text" value="0" />
<input id="discount" name="discount" type="text" value="0" />
I want substract discount from amount, and add result to another textbox. How can I do it?
$("#result").val(parseInt($("#amount").val(), 10) - parseInt($("#discount").val(), 10));
That should do it. jsFiddle here.
You can do this using JavaScript/Jquery
Suppose your third textbox is
Then your Jquery code will be
var vResult = parseInt( $('#amount').val() ) - parseInt( $('#discount').val() );
$('#result').val( vResult );
I came across this problem. It adding numbers using parseFloat or parseInt.
IF textbox1 value is 4 and textbox2 value is 2 then i got output as (see script)
My doubt is why in addition alone
parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())
gives correct value but
parseFloat($('#txt1').val() + $('#txt2').val())
is not giving correct value whereas
parseFloat($('#txt1').val() - $('#txt2').val()),
parseFloat($('#txt1').val() / $('#txt2').val()),
parseFloat($('#txt1').val() * $('#txt2').val())
are giving correct value.
Its simple but i couldn't find solution.
=====jQuery
function Calculate() { //--> Output
$('#lbl1').html(parseFloat($('#txt1').val() + $('#txt2').val())); //--> 42
$('#lbl2').html(parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())); //--> 6
$('#lbl3').html(parseFloat(4 + 2)); //--> 6
$('#lbl4').html(parseFloat($('#txt1').val() - $('#txt2').val())); //--> 2
$('#lbl5').html(parseFloat($('#txt1').val() * $('#txt2').val())); //--> 8
$('#lbl6').html(parseFloat($('#txt1').val() / $('#txt2').val())); //--> 2
}
=====HTML
<table>
<tr>
<td>
<input type="text" id="txt1" />
</td>
<td>
<input type="text" id="txt2" />
</td>
</tr>
<tr>
<td>
<input type="button" value="Calculate" onclick="Calculate()" />
</td>
<td>
<label id="lbl1">
</label>
|
<label id="lbl2">
</label>
|
<label id="lbl3">
</label>
|
<label id="lbl4">
</label>
|
<label id="lbl5">
</label>
|
<label id="lbl6">
</label>
</td>
</tr>
</table>
$.val() returns a string value.
So in your first example you convert both returned strings to numbers and the calculation is fine.
If you use parseFloat($('#txt1').val() + $('#txt2').val()) the + does not work as the arithmetic operator, but as a string concatenation. So you concatenate both strings and convert them afterwards, which gives a wrong result.
The examples using - will work, as there is no string operation using - and by thus alls values get implicitly converted to a number before the operation is applied.
$('#txt1').val() + $('#txt2').val() it gives String value
you can not use - , * , /operator on strings
parseFloat($('#txt1').val()), parseFloat($('#txt2').val()) returns numbers not strings