This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 1 year ago.
How do i write to get to decimals in the line with results? It calculates right but I want 2 decimals as the result.
<script>
function divideBy()
{
num1 = document.getElementById("odds").value;
num2 = document.getElementById("gameNumber").value;
num3 = document.getElementById("standardBet").value;
document.getElementById("result").innerHTML = ((num3 * num2)*1.9) / num1;
}
</script>
You can use toFixed()
(((num3 * num2)*1.9) / num1).toFixed(2);
Related
This question already has answers here:
Truncate (not round off) decimal numbers in javascript
(32 answers)
Closed 1 year ago.
I would like to format my numbers to always display 2 decimal places. lets say: I have a number => 21.268998 , the o/p I'm looking is to chop the rest of the decimal point and keep only the first 2 i.e:
21.26
however with the tofixed or toPrecision approach to always rounds to a certain decimal which is causing issues when a number is 99.999999, it rounds to 100.000 which is not right.
var num1 = "1";
document.getElementById('num1').innerHTML = (Math.round(num1 * 100) / 100).toFixed(2); // this is showing correctly
var num2 = "99.99999";
document.getElementById('num2').innerHTML = (Math.round(num2 * 100) / 100).toFixed(2);// this is incorrect=> I want to show 99.99
any idea how to get the first numbers to show always without rounding them off to the next number.
Jsfiidle:
https://jsfiddle.net/8ohvyczg/1/
You can use slice method in javascript:
var num2 = 99.99999;
num2 = num2.slice(0, (num2.indexOf("."))+3);
document.getElementById('num2').innerHTML = num2;
Did u tried Math.trunc()?
var num = "99.99999";
var round=(Math.round(num * 100) / 100).toFixed(2)
var trunc=(Math.trunc(num * 100) / 100)
console.log(round);
console.log(trunc);
Here's an approach of a simple truncate, and not round off:
var number = 26.4363
var str = number.toString();
console.log(str.substring(0, str.indexOf(".")+3));
Many ways to do that. But you can write a custom function to do that. I use split and substring.
const num = "99.91999";
function parseNumber(num)
{
arr = num.split('.');
return arr[0] + '.' + arr[1].substring(0,2);
}
console.log(parseNumber(num))
This question already has answers here:
Javascript: Round up to the next multiple of 5
(10 answers)
Closed 1 year ago.
I have such a problem, I want to round the given numbers to the nearest high number that is divisible by 5.
For example:
let num = 12;
function round5(arg) {
console.log(Math.round(arg / 5) * 5)
}
round5(num)
In this case, I want the result to be 15.
What would be the best solution for this? Thanks in advance.
round will round to the closest integer so 2.4 becomes 2. You want to use ceil which always round up so 2.4 becomes 3
let num = 12;
function round5(arg) {
console.log(Math.ceil(arg / 5) * 5)
}
round5(num)
you should use Math.ceil which will round up
function round5(arg) {
console.log(Math.ceil(arg / 5) * 5)
}
This question already has answers here:
How to get numeric value from a prompt box? [duplicate]
(6 answers)
Javascript: "+" sign concatenates instead of giving sum of variables
(4 answers)
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
Closed 2 years ago.
I am trying this code to add a number starting from 1 to n using this mathematical equation:n = n(n+1) /2
var n = prompt("enter a number");
function adding(n) {
let N = n * (n + 1) / 2;
return N;
}
alert(adding(n));
but it's not working in prompt. It works like this:
function adding(n) {
let N = n * (n + 1) / 2;
return N;
}
console.log("answer", adding(100));
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have an issue with the subtraction of two values. when I set discount_amt value to 2.5 then total return me 0.5 but when discount_amt is set to 2.6 it return 0.3999999999999999 instead of 0.4 why?
var total = parseFloat('3').toFixed(1);
var discount_amt = parseFloat('2.6').toFixed(1);
total = total - discount_amt;
console.log(total);
var total = parseFloat('3').toFixed(1);
var discount_amt = parseFloat('2.6').toFixed(1);
total = total - discount_amt;
console.log(total);
This seems to fix it. You forget parsefloat() and tofixed()
total = 3;
discount_amt = 2.6;
console.log(parseFloat(total).toFixed(1) + ' ' + parseFloat(discount_amt).toFixed(1));
total = parseFloat(total).toFixed(1) - parseFloat(discount_amt).toFixed(1);
console.log(parseFloat(total).toFixed(1));
Explanation why floats are handled this way: answer or directly to the link that answer refers to link
This question already has answers here:
Javascript roundoff number to nearest 0.5
(10 answers)
Closed 7 years ago.
I'm looking for an output of
4.33 = 4.5
4.5517263843648 = 5
Using the Math.round function in javascript. I've tried a few methods but neither seem to work.
Try to use,
function test(val) {
var x = Math.floor(val);
return (val - x) > 0.5 ? Math.ceil(val) : (x + 0.5);
}
DEMO