I would like to round up an integer with Javascript.
I have a JSON that retrieve an amount e.g 7435 but I want to round it up to 7500, so I can use in an simple math function. Any ideas? Cause round, and ceil are working only with decimals.
You can use the Math.ceil function
Math.ceil(7435/100)*100;
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil
You can use this formula:
x = Math.floor((x+99) / 100)*100;
or
x = Math.ceil(x/100) * 100;
or go learn some Math :-)
Math.ceil( number / 100 ) * 100 should work. If you do Math.ceil(7435/100) it will give you 75 and multiplying by 100 will give you 7500. Ceil does not only work with decimals.
var rounded = Math.ceil( 7435 / 100) * 100;
console.log(rounded);
Related
I have a function that calculates percentage increase of 2 numbers:
const 1st_num = 50
const 2nd_num = 100
percentage = ((1st_num - 2nd_num) / 1st_num) * 100 // -100
It seems correct but what if the 1st number is 1?
((1 - 50) / 1) * 100 // -4900
I don't see it making sense anymore. What am I missing?
If you are computing a delta variation in percentage between 2 numbers, it should be the other way around:
variation = ((num2 - num1) / num1) * 100
Last but not least, your delta can be over 100%
For example, imagine at
t1=10 and t2=11 -> your delta will be computed like this : (11 - 10)/10, so you have an increase of 10%
but if you have t1=10 and t2=100 -> your delta will become (100 - 10)/10, so you have an increase of 900%
First up all your question is more suitable to somewhere in math forums:
Your formula is right just change it as follows to get increase change in positive numbers:
percentage = ((2nd_num - 1st_num) / 1st_num) * 100 // 100%
However your treatment with 1 is exactly right.
4900 % In other words 49 times increase in value.
You can't use variable name starts with numbers
const fst_num = 50
const snd_num = 100
percentage = ((snd_num -fst_num) / fst_num) * 100
const fst_num = 70192.32
const snd_num = 17548.08
const percentage = ( 100 - ( ( fst_num - snd_num ) / fst_num ) * 100 );
So I am trying to take a number that can be in any positive form and cut it to two decimal places. So for example I have an input of 145.26. However in my code this is being rounded down to 145.19. Here is the code I am using:
var multiplier = 100;
var adjustedNum = input * multiplier;
var truncatedNum = Math[adjustedNum < 0 ? 'ceil' : 'floor'](adjustedNum);
var fixedResult = truncatedNum / multiplier;
So basically my 'input' should become 145200. However it is actually becoming 145199.9999995 or something to that effect. This is causing the Math.floor method to round it down. Is there any way to workaround or avoid this?
Multiply the number by an additional factor of 10 to round it. Then divide it by 10 to apply the floor or ceil.
var multiplier = 100;
var adjustedNum = Math.round(input * multiplier * 10);
var truncatedNum = Math[adjustedNum < 0 ? 'ceil' : 'floor'](adjustedNum/10);
var fixedResult = truncatedNum / multiplier;
I am trying to round a value in JS but I am getting not rounded value, this is what I have:
$(document).on("click", ".open-AddBookDialog", function () {
var agentPercentage = parseFloat($('#<%=ddlSplitPerc.ClientID%>').val()).toFixed(2);
var percMod = 1.0 - agentPercentage;
percMod = Math.ceil(percMod * 100) / 100;
var dropdownAgentPerc = $('#<%=ddlPercSplitAgent.ClientID %>');
dropdownAgentPerc.val(percMod);
dropdownAgentPerc.change();
$('#AddNewSplitAgentLife').modal('show');
});
For example, the agentPercentage is 0.7 and when I am subtracting 1 - 0.7 I am getting this value:
0.30000000000000004
What do you think I should change? I tried the Math.cell example as well but I am getting 0.31 as a result.
The solution is in another question already asked: Dealing with float precision in Javascript
(Math.floor(y/x) * x).toFixed(2);
It should work if you subtract .5 from the value you pass to Math.ceil
percMod = Math.ceil((percMod * 100) -.5 )/ 100;
Math.ceil will round up for any decimal above .000
So to simulate the typical rounding behavior of only rounding decimals above .500 you should subtract .5
in javascript, lets say I got a random number 136, i'd like it to convert it automatically to 140 or if I got 124 to 120 etc or 24 to 20 etc..
Divide by 10 and round it, then multiply by 10.
var x = 136;
console.log(Math.round(x / 10) * 10);
I would just do this: ( Math.round(YourNumber/10) ) * 10
is there a better way to multiply and divide figures than using the * and / ?
There is a strange behavior in Chrome Firefox and Internet Explorer using those operaters:
x1 = 9999.8
x1 * 100 = 999979.9999999999
x1 * 100 / 100 = 9999.8
x1 / 100 = 99.99799999999999
http://jsbin.com/ekoye3/
I am trying to round down the user input with parseInt ( x1 * 100 ) / 100 and the result for 9999.8 is 9999.79
Should I use another way to achieve this?
That's no bug. You may want to check out:
Is JavaScript’s math broken?
Integer arithmetic in floating-point is exact, so decimal representation errors can be avoided by scaling. For example:
x1 = 9999.8; // Your example
console.log(x1 * 100); // 999979.9999999999
console.log(x1 * 100 / 100); // 9999.8
console.log(x1 / 100); // 99.99799999999999
x1 = 9999800; // Your example scaled by 1000
console.log((x1 * 100) / 10000); // 999980
console.log((x1 * 100 / 100) / 10000); // 9999.8
console.log((x1 / 100) / 10000); // 99.998
You could use the toFixed() method:
var a = parseInt ( x1 * 100 ) / 100;
var result = a.toFixed( 1 );
You may want to check this. If you want to do computation on numbers which represent money, you should count cents and use integers.