How to round a whole number in javascript - javascript

How to round an integer in javascript to its previous decimal.
Ex.:
15 to 10.
16 to 10.
21 to 20.
29 to 20.

Can even use Math.floor()
var num = 24;
var round = Math.floor(num / 10) * 10;
console.log(round)

This should do the trick
parseInt(15/10) * 10

you can create your own function to do that
function intFloor(num){
let temp = num%10;
return num-temp;
}
console.log(intFloor(15));
if you know prototype You can add this to prototype
Number.prototype.intFloor = function(){
let temp = this%10;
return this - temp;
}
console.log((52).intFloor()) // result 50

Related

Rounding-off from whole numbers to whole numbers in JavaScript?

So I have some numbers x = 320232 y = 2301 z = 12020305. I want to round these numbers off using JavaScript so that they become x = 320000 y = 2300 z = 12000000.
I tried Math.round and Math.floor but turns out that they only work with decimal values like
a = 3.1; Math.round(a); // Outputs 3 and not whole numbers.
So my question is can we round of whole numbers using JavaScript and If yes then how?
Edit: I want it to the round of to the starting 3 digit places as seen in the variables above. Like If there was another variable called c = 423841 It should round off to become c = 424000.
You could work with the logarithm of ten and adjust the digits.
const
format = n => v => {
if (!v) return 0;
const l = Math.floor(Math.log10(Math.abs(v))) - n + 1;
return Math.round(v / 10 ** l) * 10 ** l;
};
console.log([0, -9876, 320232, 2301, 12020305, 123456789].map(format(3)));
The solution is to first calculate how many numbers need to be rounded away, and then use that in a round.
Math.round(1234/100)*100 would round to 1200 so we can use this to round. We then only need to determan what to replace 100 with in this example.
That is that would be a 1 followed by LENGTH - 3 zeros. That number can be calculated as it is 10 to the power of LENGTH - 3, in JS: 10 ** (length - 3).
var x = 320232;
var y = 2301;
var z = 12020305;
function my_round(number){
var org_number = number;
// calculate integer number
var count = 0;
if (number >= 1) ++count;
while (number / 10 >= 1) {
number /= 10;
++count;
}
// length - 3
count = Math.round(count) - 3;
if (count < 0){
count = 0;
}
// 10 to the power of (length - 3)
var helper = 10 ** count;
return Math.round(org_number/helper)*helper;
}
alert(my_round(x));
alert(my_round(y));
alert(my_round(z));
It is not the prettiest code, though I tried to make it explainable code.
This should work:
function roundToNthPlace(input, n) {
let powerOfTen = 10 ** n
return Math.round(input/powerOfTen) * powerOfTen;
}
console.log([320232, 2301,12020305, 423841].map(input => roundToNthPlace(input, 3)));
Output: [320000, 2000, 12020000, 424000]

Round a float up to the next integer in javascript

I need to round floating point numbers up to the nearest integer, even if the number after the point is less than 0.5.
For example,
4.3 should be 5 (not 4)
4.8 should be 5
How can I do this in JavaScript?
Use the Math.ceil[MDN] function
var n = 4.3;
alert(Math.ceil(n)); //alerts 5
Use ceil
var n = 4.3;
n = Math.ceil(n);// n is 5
Round up to the second (0.00) decimal point:
var n = 35.85001;
Math.ceil(n * 100) / 100; // 35.86
to first (0.0):
var n = 35.800001;
Math.ceil(n * 10) / 10; // 35.9
to integer:
var n = 35.00001;
Math.ceil(n); // 36
jsbin.com
Use
Math.ceil( floatvalue );
It will round the value as desired.

How to strip 25 to just 5 using javascript?

if i have a number like 25 for example, i would like to use javascript to "knock off" the 2 and keep the 5.
Same for any number - i want the 9 from the 89.
But i also want it to not do this if the number is single like an 8 for example
Thanks in advance,
Reece
Use the modulo operator, it returns the "rest" after a division:
lastDigit = number % 10;
You could use modulus:
var number = 89;
var reminder = number % 10; // 9
You can use the modulus operator (%):
var num = 89 % 10; // 9
var number = 25;
var singledigit = number%10;

Round money to nearest 10 dollars in Javascript

How can I round a decimal number in Javascript to the nearest 10? My math is pretty rubbish today, it could be the 2 hour sleep :/
Some sample cases
$2823.66 = $2820
$142.11 = $140
$9.49 = $10
I understand I probably need a combination of Math.round/floor but I can't seem to get expected result.
Any help/pointers appreciated!
M
Try
Math.round(val / 10) * 10;
Use this function:
function roundTen(number)
{
return Math.round(number/10)*10;
}
alert(roundTen(2823.66));
To round a number to the nearest 10, first divide it by 10, then round it to the nearest 1, then multiply it by 10 again:
val = Math.round(val/10)*10;
This page has some details. They go the other way (e.g., rounding to the nearest 0.01) but the theory and practice are identical - multiply (or divide), round, then divide (or multiply).
10 * Math.round(val / 10)
function round(number, multiplier) {
multiplier = multiplier || 1;
return Math.round(number / multiplier) * multiplier;
}
var num1 = 2823.66;
var num2 = 142.11;
var num3 = 9.49;
console.log(
"%s\n%s\n%s", // just a formating thing
round(num1, 10), // 2820
round(num2, 10), // 140
round(num3, 10) // 10
);

Round a number to nearest .25 in JavaScript

I want to convert all numbers to the nearest .25
So...
5 becomes 5.00
2.25 becomes 2.25
4 becomes 4.00
3.5 becomes 3.50
Here’s an implementation of what rslite said:
var number = 5.12345;
number = (Math.round(number * 4) / 4).toFixed(2);
Multiply by 4, round to integer, divide by 4 and format with two decimals.
If speed is your concern, note that you can get about a 30% speed improvement by using:
var nearest = 4;
var rounded = number + nearest/2 - (number+nearest/2) % nearest;
From my website: http://phrogz.net/round-to-nearest-via-modulus-division
Performance tests here: http://jsperf.com/round-to-nearest
Here is a generic function to do rounding. In the examples above, 4 was used because that is in the inverse of .25. This function allows the user to ignore that detail. It doesn't currently support preset precision, but that can easily be added.
function roundToNearest(numToRound, numToRoundTo) {
numToRoundTo = 1 / (numToRoundTo);
return Math.round(numToRound * numToRoundTo) / numToRoundTo;
}
Here is #Gumbo's answer in a form of a function:
var roundNearQtr = function(number) {
return (Math.round(number * 4) / 4).toFixed(2);
};
You can now make calls:
roundNearQtr(5.12345); // 5.00
roundNearQtr(3.23); // 3.25
roundNearQtr(3.13); // 3.25
roundNearQtr(3.1247); // 3.00
function roundToInc(num, inc) {
const diff = num % inc;
return diff>inc/2?(num-diff+inc):num-diff;
}
> roundToInc(233223.2342343, 0.01)
233223.23
> roundToInc(505, 5)
505
> roundToInc(507, 5)
505
> roundToInc(508, 5)
510
Use below function, hope it helps
function roundByQuarter(value) {
var inv = 1.0 / 0.25;
return Math.round(value * inv) / inv;
}
Call the function as below, will result the nearest Quarter value, that is it will not return .32, .89, .56 but will return .25, .75, .50 decimals only.
roundByQuarter(2.74) = 2.75
roundByQuarter(2.34) = 2.25
roundByQuarter(2.94) = 3.00
roundByQuarter(2.24) = 2.25
A very good approximation for rounding:
function Rounding (number, precision){
var newNumber;
var sNumber = number.toString();
var increase = precision + sNumber.length - sNumber.indexOf('.') + 1;
if (number < 0)
newNumber = (number - 5 * Math.pow(10,-increase));
else
newNumber = (number + 5 * Math.pow(10,-increase));
var multiple = Math.pow(10,precision);
return Math.round(newNumber * multiple)/multiple;
}

Categories

Resources