Round down to full hundred [duplicate] - javascript

This question already has answers here:
Round a number to nearest .25 in JavaScript
(8 answers)
Closed 9 years ago.
Hi there I want to round down an amount to full hundred:
Examples
23009 rounded 23000,
23099 rounded 23000,
23199 rounded 23100,
My function do not work, because it rounds mathematically.
$("#span_berechnungsgrundlage").text(berechnungsgrundlage);
var berechungsgrundlage_gerundet = Math.round(berechnungsgrundlage / 100) * 100;
alert(berechungsgrundlage_gerundet);
Thank you for your help

Use Math.floor.
Math.floor - Round a number downward to its nearest integer:
var berechungsgrundlage_gerundet = Math.floor(berechnungsgrundlage / 100) * 100;

You can use Math.floor:
Math.floor(number / 100) * 100;
Demo

Just replace Math.round with Math.floor.

Related

Javascript equiv to FLOOR in excel with 0.5 values [duplicate]

This question already has an answer here:
Math.floor () to round down to nearest 0.5 in javascript [duplicate]
(1 answer)
Closed 3 years ago.
I do NOT want to round to the NEAREST 0.5. I want to round DOWN to the nearest 0.5 value.
I have a function from excel that I use: =FLOOR(value,0.5)
9.55 becomes 9.5
9.99 becomes 9.5
9.2 becomes 9
Etc.
Is there an equivalent in javascript? I know that Math.floor () will round down to the nearest integer. However, does anyone know of a good way to round down to the nearest 0.5 instead?
Thank you in advance
Return the whole number + 0.5, if the original decimal value is >= 0.5.
const floor = (value) => {
let wholeNumber = Math.floor(value)
return wholeNumber + ((value - wholeNumber) < 0.5 ? 0.0 : 0.5)
}
This can be modified to support nearly any fraction other than 0.5.
const assertEquals = (n1, n2, precision = 0.001) => Math.abs(n1 - n2) <= precision
/**
* Returns the largest integer less than or equal to a given number rounded down to the nearest fraction.
* #param {number} value - A positive floating-point value
* #param {number} [nearest=1.0] - A fractional value between (0.0, 1.0]
* #return A number representing the largest integer less than or equal to the specified number rounded down to the nearest fraction
*/
const floorNearest = (value, nearest = 1.0) => {
let wholeNumber = Math.floor(value)
let fraction = value - wholeNumber
let factor = Math.floor(fraction / nearest)
return wholeNumber + (nearest * factor)
}
console.log(assertEquals(floorNearest(1.50), 1))
console.log(assertEquals(floorNearest(1.25), 1))
console.log(assertEquals(floorNearest(9.55, 0.5), 9.5))
console.log(assertEquals(floorNearest(9.99, 0.5), 9.5))
console.log(assertEquals(floorNearest(9.20, 0.5), 9.0))
console.log(assertEquals(floorNearest(0.55, 0.200), 0.400))
console.log(assertEquals(floorNearest(0.55, 0.250), 0.500))
console.log(assertEquals(floorNearest(0.99, 0.333), 0.666))
.as-console-wrapper { top: 0; max-height: 100% !important; }

Generating random number in javascript [duplicate]

This question already has answers here:
Javascript Random Number?
(3 answers)
Closed 3 years ago.
i am using this function to generate random number between 1000 and 100.
but here according to me, in (max - min) + min, max- min =900 and min= 100, so it should not generate numbers between 900 and 100? but it is returning numbers greater than 900 also how? I am confused. and do tell how to check the range for the numbers random function is generating? any help with this?
x = Math.floor(Math.random() * (1000 - 100) + 100);
console.log(x);
The formula for random numbers Math.random() * (max - min) + min is the correct one to get a uniformly distributed number between min and max.
max - min will give you the range in which you want to generate the random numbers. So in this case 1000 - 100 results in a range of 900.
Multiplying by Math.random() will give you a random number in the range. So, with a Math.random() producing 0.5 after multiplying you get 450.
Finally, adding min back to the random pick ensures the number you get is within bounds of min and max.
For example Math.random() produces 0.01 if we substitute in the formula we get 0.01 * (1000 - 100) = 9 which is below min. Conversely, if Math.random() produces 1 then 1 * (1000 - 100) = 900 which is the highest random number possible to get from the range and yet it's still below max. In both cases adding min to the result ensures the random number you get is within max and min
The function Math.random() returns a number between 0 and 1.
When use "Math.random() * (1000 - 100)", this part of the code generates a number between 0 and 1 then multiplies it by 900, which will give you a number between 0 and 900.
Now in the last block you do add 100 to the previously generated number which results in a number between 0 and 900 + 100, which gives a result between 100 and 1000.
function random(min, max) {
console.log("Multiplying by: " + (max - min));
console.log("And adding : " + min);
return Math.floor(Math.random() * (max - min) + min);
}
console.log(random(100, 1000));
Multiply by (1000 -200) instead as you already have +100
Because in case random number generated is anything greater than 800 you end exceeding range as you're adding 100 in it everytime
x = Math.floor(Math.random() * (1000 - 200) + 100);
console.log(x);
Thumb rule :-
Math.floor(Math.random() * - ( max - ( 2 * min ) ) + min )
As Math.random() generate floats, this need to be converted to an integer.
We can use parseInt(), but there is a shorthand, the ~~ bitwise operator. Performances are known to be excellent.
console.log(
100 + ~~(Math.random() * 800)
)
One possible alternative is the web crypto api, it might be a bit slower, but with the best randomness doable. This return an integer between 0 and 256.
console.log(
100 + ~~(crypto.getRandomValues(new Uint8Array(1))[0] * 3.13)
)

Math: Formula to scale a number to another number

I asked a similar question earlier today, and it turns out that I just suck at math, because I can't figure this one out, either.
I'm calculating the screen ratio via width/height. I need a function to convert that resulting number to a new scale.
e.g.
function convertNum(ratio) {
return //formula here
}
Examples:
Given a resolution of 3000x1000 = ratio of 3 (i.e. 3000/1000).
I want it converted to 133.3 via the function, e.g. convertNum(3) spits out 133.33
2500x1000 = 2.5 (desired result: 100)
2000x1000 = 2 (desired result: 66.6)
1500x1000 = 1.5 (desired result: 33.3)
1000x1000 = 1 (desired result: 0)
It should keep scaling this way for all screen ratios above 1.0.
You need to add an additional 33.3% for every 0.5 in the ratio.
First figure out how many "padding pieces" you need to add:
// Subtracting 1 since 1 should result in a 0
(ratio - 1) / 0.5
Then multiply the number of padding pieces by the padding amount:
((ratio - 1) / 0.5) * 0.333
But dividing by 0.5 is the same thing as multiplying by 2, so it can be further reduced down to:
(ratio - 1) * 2 * 0.333
But that's obviously the same as:
(ratio - 1) * 0.666
Although, you could get more precision by changing that to:
(ratio - 1) * (2 / 3)

How to round down number 2 decimal places? [duplicate]

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 6 years ago.
How to get following inputs to bellow outputs
Input
1.0789
10.350
1.7777
Output
1.07
10.35
1.77
Use Math.floor to round the decimal places under the current value.
Reference
Example
Math.floor(1.0789 * 100) / 100
Working Fiddle
console.log(Math.floor(1.0789 * 100) / 100);
console.log(Math.floor(10.350 * 100) / 100);
console.log(Math.floor(1.7777 * 100) / 100);
console.log(Math.floor(12.34 * 100) / 100);
you have several methods for do this
Use Math.round(num * 100) / 100
Use Math.ceil(num * 100)/100;

Explain the * 2 in Math.floor(Math.random() * 2); [duplicate]

This question already has answers here:
Explain Math.floor(Math.random())
(6 answers)
Closed 7 years ago.
Is this the most efficient way of randomly generating 0 or 1 in javascript?
Math.floor(Math.random() * 2);
No, Math.random() * 2 | 0 and Math.random() * 2 << 0 seem to be (marginally) faster in most browsers, which makes sense since it has one less function invocation from the Math library.
Faster alternatives:
(Math.random() * 2) | 0
~~ (Math.random() * 2)
(Math.random() * 2) << 0

Categories

Resources