Math.Round Javascript [duplicate] - javascript

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

Related

2 decimals in function in Javascript [duplicate]

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);

Problem calculating the sum of arithmetic progression when using prompt() [duplicate]

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));

Reverse Integer Without converting to a string Javascript? [duplicate]

This question already has answers here:
JavaScript: How to reverse a number?
(19 answers)
Closed 2 years ago.
So i am trying to reverse an integer and so far the code works but i am trying to find a solution to reverse the integer without converting into a string first? Any help would be appreciated. This is my code snippet so far to reverse the integer.
function reverseInt(num) {
const reversed = num.toString().split('').reverse().join('')
return parseInt(reversed) * Math.sign(num)
}
console.log(reverseInt(-500));
I am trying to do it using javascript.
Try this:
function reverseInt(number) {
var isNegative = number < 0 ? true : false;
if(isNegative)
number = number * -1;
var reverse = 0, lastDigit = 0;
while (number >= 1) {
reverse = Math.floor(reverse * 10 + (number % 10));
number = number / 10;
}
return isNegative == true ? reverse*-1 : reverse;
}
console.log(reverseInt(-500));
console.log(reverseInt(501));

How subtraction of two float values in JavaScript? [duplicate]

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

How to round figure if it is less then point 5 only in javascript [duplicate]

This question already has answers here:
Javascript roundoff number to nearest 0.5
(10 answers)
Closed 7 years ago.
I am trying to round my figure in JavaScript if it is less then any 0.5 figure. For Instance if I have value 3.4 then it should be 3 but if 3.5 then there is no need to round figure. I only want to round figure if it is less than 0.5. Here is the code I am using to round figure. But it rounded the figure in both cases
This code gives me result 3.5
function rounded(){
var val = 3.5;
var rounded = Math.round(val);
console.log("rounded",rounded);
}
and this code gives me result 4 but I want 3.5..
function rounded(){
var val = 3.6;
var rounded = Math.round(val);
console.log("rounded",rounded);
}
Any body can help me with this?
Use Math.floor to round down.
var round = function (num) {
return Math.floor(num * 2) / 2;
};
console.log(round(3)); // 3
console.log(round(3.4)); // 3
console.log(round(3.5)); // 3.5
console.log(round(3.6)); // 3.5
console.log(round(4)); // 4
This should truncate to the nearest .5:
parseInt(x * 2) / 2;

Categories

Resources