Getting a number divisible by five with Math.Round - javascript

I have a number variable that is between 0 and 100. It ccould be something like 83.333334.
I want to use Math.Round to round the number (e.g. Math.round(83.333334);). How can I do this so that the result is always divisible by five (i.e. in the set [0, 5, 10, 15... 85, 90, 95, 100])?

Divide by 5, round it, multiply by 5.
alert(Math.round(83 / 5) * 5);
jsFiddle Demo

function roundDownToMultiple(number, multiple) {
return number - (number % multiple);
}
roundDownToMultiple(86, 5); // 85
roundDownToMultiple(89, 5); // 85
roundDownToMultiple(96, 5); // 95

Use the modulus operator to "round" down your number to a multiple of 5, see the example below.
var x = Math.round(83.333334);
x = x - (x % 5);
If you'd like to "round towards zero" (and have a correct value for negative numbers aswell) use something like this:
x = Math[x < 0 ? 'ceil' : 'floor'] (x/5) * 5;

Using this Math.round(Math.floor(Math.random() * 100) / 5) * 5 You can get the Numbers divisible by 5.
100 - is the range of the Result.

Give this a try.
Math.round(val / 5) * 5;

Related

Truncate to the nearest 50 in JavaScript

What JavaScript formula can I use to truncate a number to the nearest 50.
Example. I wanted 498 > 450
I have tried
Math.round (498, 50 )
And
Math.ceil(498, 50)
But am not getting. Please help
This may be a mixup of terminology, mixing terms like "nearest" and "truncate", neither of which quite describes what the example demonstrates.
The example you give always rounds down, never up, to the nearest custom value (in this case 50). To do that you can just subtract the result of % 50. For example:
const val = 498;
console.log(val - val % 50);
Even make it a re-usable function:
const Nearest = (val, num) => val - val % num;
console.log(Nearest(498, 50));
Divide by 50, do the operation, multiply by 50.
console.log(Math.floor(498 / 50) * 50);
console.log(Math.ceil(498 / 50) * 50);
console.log(Math.round(498 / 50) * 50);
console.log(Math.trunc(498 / 50) * 50);
You divide your number by 50, take the ceiling of that number and then multiply it by 50.
Math.ceil(value / 50) * 50;
A quick sidenote: truncate has a whole other meaning for numbers in Javascript: Math.trunc on MDN
Edit:
If you want other rounding semantics than ceil you can of course use floor (always goes to lowest multiple of 50):
Math.floor(451 / 50) * 50; // => 450
You divide by the multiple and round and then multiply by the multiple. If you want the lower bound, you use floor instead of round. If you want the upper bound, you use ceil instead of round. Look at these examples:
let x = 498;
let y = Math.round(498/50)*50;
console.log(y);
y = Math.floor(498/50)*50;
console.log(y);
y = Math.ceil(498/50)*50;
console.log(y);
To do what you want, the Remainder operator is your best friend. This will give you whatever is left over after dividing the number by the nearest number.
If your goal is to always round down, the following function would work. Just take your original number, find the remainder, and remove the remainder:
function roundDownToNearest(num, nearest){
return num - (num % nearest);
}
console.log(roundDownToNearest(498, 50))
If you always want to round up, you round down, then add the nearest amount:
function roundUpToNearest(num, nearest){
return num - (num % nearest) + nearest;
}
console.log(roundUpToNearest(498, 50))
If you want to get to the closest of the two, you could do the following. Find your remainder, then see if it's greater or less than half of your nearest value. If it's greater, round up. If less, round down.
function roundToNearest(num, nearest){
if(num % nearest > nearest / 2){
return roundUpToNearest(num, nearest);
} else {
return roundDownToNearest(num, nearest);
}
}
console.log(roundToNearest(498, 50))
console.log(roundToNearest(458, 50))

Calculate the percentage of a value in a logarithmic scale

I have a logarithmic scale going from 0 to 100:
0.00
0.10
1.00
10.00
100.00
I need to make a pie chart which has 4 quarters.
the first is going from 0 to 0.10
the second is from 0.10 to 1 etc.etc.
So if I have the value 25, it should be calculated which percentage this is in the logarithmic scale. Considering the scale it should end up somewhere in the last quarter of the chart.
Unfortunately my understanding of Maths does not reach this far ;)
Could you help out and tell me where to start.
I thought of looking at each quarter as a 100% piece, and then calculate where this might be in this quarter..
per example:
32 > 10 so it should be in the last quarter (percentage wise above 75%)
So in this last quarter 32 will be in:
((32-10) x 100) / (100 - 10) = 24.44% in this quarter
Making this 24.44 / 4 = 6.11% over 4 quarters and thus 75 + 6.11 = 81.11% of the whole chart.
Now this would work, but I am looking for a shorter and simpler way of calculating this.
Can you please help out.
This is surely a maths question about plotting values on a logarithmic
scale, and not really anything to do with JavaScript in particular or
programming in general. Anyhow ...
You need to decide on a minimum value, since the logarithm of zero is
undefined. Once you have your maximum and minimum logarithms, you can
scale your values as you wish. Slightly ontopic: JavaScript has
Math.log10 in more up-to-date engines (and can be readily
defined if not, e.g. as in #NinaScholz's answer or using the polyfill here).
var minval = 0.01,
maxval = 100,
minlog = Math.log10(minval),
maxlog = Math.log10(maxval),
range = maxlog - minlog,
lineartolog = function(n){
return (Math.log10(n) - minlog) / range;
},
logplots = [
0.01,
0.1,
1,
3.2,
10,
32,
75,
100
].map(lineartolog);
document.body.innerHTML = '<pre>' + logplots + '</pre>';
Adjust as required for percentages, radians, etc.
Consideration:
q0 q1 q2 q3
01234567890123456789012345678901234567890
| | | | |
0.01 0.1 1 10 100
0.32 3.2 32
because log10(32) = 1.505 = 1 + 0.505
because log10(3.2) = 0.505 = 0 + 0.505
because log10(0.32) = -0,495 = -1 + 0.505
^ ^
quadrant after adding 2 amount to fill
together, fill is the amount in %:
function log10(f) {
return Math.log(f) / Math.log(10);
}
function getValue(v) {
var l = log10(v),
quadrant = Math.floor(l) + 2,
fill = (l - Math.floor(l)) * 100;
return { quadrant: quadrant, fill: fill };
}
console.log('0.32', getValue(0.32));
console.log('3.2', getValue(3.2));
console.log('32', getValue(32));

Round numbers in Javascript before decimal point

How do you round down a number before the decimal points
So not 45.1 -> 45
But 47 -> 45
Or 45 -> 50
Try like this:-
Math.round(45/ 10) * 10;
var number = 45.5;
alert(Math.round(number / 5) * 5);
This rounds to nearest 5.
var number = 45.5;
alert(Math.round(number / 10) * 10);
And this to nearest 10.
There is also a function floor that rounds to lower number and ceil that rounds to higher number` for example:
var number = 45.5;
alert(Math.floor(number / 10) * 10); // This will give 40
alert(Math.ceil(number / 10) * 10); // This will give 50

Rounding in steps of 20 (or X) in JavaScript?

I have numbers starting at 50 and ending with 190. The steps are alwyas 20 -->
50, 70, 90, .... 190
Now I get a number from a text filed which has to be rounded according to this. So if I get 55, it should become 70, if I get 77, it should be 90. If I get 90, it should of course stay 90. I know how to do it steps of 10:
// 55 --> 60
number = Math.ceil(number / 10) * 10
How to round the number I get using steps of 20?
Thanks!
If you want to round in increments of x, with an offset:
function round(number, increment, offset) {
return Math.ceil((number - offset) / increment ) * increment + offset;
}
round(51, 20, 10) // 70
round(70, 20, 10) // 70
round(99, 20, 10) // 110
round(100, 20, 10) // 110
function round(number,x,o) {
o = ~~(o);
return o + Math.ceil((number - o)/ x ) * x
}
console.log(round (55,20,10)) //70
console.log(round (77,20,10)) //90
console.log(round (90,20,10)) //90
#Cerbrus thx to point that out
Perhaps with something like that
number = Math.ceil( (number - 50) / 20 ) * 20 + 50;
var number;
var a =number-10-((number-10)%20);
var result=a+30;
// does not work if num is 70 or 90 so
if(result-20==number)
Result=number;
Or as a one-liner:
var result=number+20-((number-10)%20) - result-20==number : 0 ? 20;
The logic is that first, you add 20 to the number. Take the number 77. 77+20=97. But, 77+20-7=90, what you want. And 7 is the difference between 77 and 70, the previous valid number. You can get the differencewith (number-10)%20. However, it doesn't work if number is OK to start out with, hence the logic at the end.

JavaScript - Incorrect division

perc = 15/30;
//result=Math.round(perc*100)/100 //returns 28.45
$('#counter').text(perc);
$('#total').text(count);
returns back 0.5% which is suppose to be 50.00%... how do I fix this? :S
You do realize that word percent quite literally translates into "per cent" or "per 100" since cent is the latin root that's used everywhere meaning "100" or "one-hundredth".
Century (100 years)
US Cent (100th of a dollar)
Centurion (Those who commanded 100 soldiers)
Centipede (creature with 100 legs)
So 50% becomes 50 per cent becomes 50 per 100
And, since in mathematical terms, the word per means divide (miles per hour == mph == m/h) then we can distill 50% down to:
50/100
Which, surprisingly enough, is represented as the decimal number .5
15/30 = 0.5
if you want to have percent number you have to multiply it by 100.
I am a low rep user so here goes. http://en.wikipedia.org/wiki/Percentage
Treat the % sign as a constant equal to 0.01. Thus, when working with a number like 50%, treat it as 50 * 0.01 or 0.5.
0.5 = n % // I want to know what 0.5 is as a percent
0.5 / % = n * % / % // Divide both sides by the constant
0.5 / % = n // Remove the excess
0.5 / 0.01 = n // Replace the constant
50 = n // You have your answer
Just multiply by 100.

Categories

Resources