Math.ceil not working with negative floats - javascript

I am trying to create a RoundUp function with help of Math.ceil it working fine with positive number but do not round up the negative numbers
Here is what i am trying
var value = -12.369754; --> output = -12
// make value = 12.369754; and out put will be 13
var decimalPoints = 0;
if (decimalPoints == 0) {
value = Math.ceil(parseFloat(value));
}
console.log(value);
Here is the Fiddle http://jsfiddle.net/n7ecyr7h/
Why This function?
I need to create a function in which user will give a number and decimal points upto which he wants to round the number The RoundUp function will roundUp the given value to a given number of decimal points
For example if user enters 12.12445 and wants to roundUp to 3 decimal points the output will be 12.125
Here is a table of required outputs with 2 decimal points
**Input** **output**
1.2369 1.24
1.2869 1.29
-1.1234 -1.13
-1.17321 -1.18
And here is the Updated Fiddle with original JS code http://jsfiddle.net/n7ecyr7h/1/

The Math.ceil method does actually round up even for negative values. The value -12 is the closest integer value that is at higher than -12.369754.
What you are looking for is to round away from zero:
value = value >= 0 ? Math.ceil(value) : Math.floor(value);
Edit:
To use that with different number of decimal points:
// it seems that the value is actually a string
// judging from the parseFloat calls that you have
var value = '-12.369754';
var decimalPoints = 0;
// parse it once
value = parseFloat(value);
// calculate multiplier
var m = Math.pow(10, decimalPoints);
// round the value
value = (value >= 0 ? Math.ceil(value * m) : Math.floor(value * m)) / m;
console.log(value);
Demo: http://jsfiddle.net/Guffa/n7ecyr7h/3/

Math.ceil(-1.1234) will be -1.12 because in negative -1.12 > -1.1234.
I think you misunderstood mathematically.

Related

How do I shift a number x decimal places to the left or right and remove minus sign?

In Google Sheets Script, how can I take a value from a cell and then shift it e.g. 2 decimal places to the left or right and also remove the minus sign.
For example if I take a value from a cell that is -0.25 how can I transform it to 25 and then use this value in the script.
Thanks
You could take the absolute value of the multiplication of the value with 100.
var value = -0.25,
result = Math.abs(value * 100);
console.log(result);
var x=-.25;
console.log(Math.sqrt(x*x)*100);
I would do it like:
function myFunction() {
var value = -0.2512
// Get the numbers after the '.', and count them
var decimals = value.toString().split('.')[1].split('').length;
// Multiply 10 for the amount of numbers after the '.'
var multiplier = Math.pow(10, decimals)
var result = Math.abs(value * multiplier);
Logger.log(result);
}
So you can have either 0.2 or 0.222222 because it counts how many numbers go after the coma and then uses it to know if you have to multiply by 10, 100, 1000, etc.

How do i return a number excluding the last digit?

So I have a number like 5467. I want my code to return 546.
I tried taking the last number and subtracting it from the original number but I get 5460 instead of 546.
Combine / with %:
(5467 - (5467 % 10)) / 10
564
Sounds like you also need to divide my 10. You could do something like this:
var number = 5467;
number = number - (number % 10); // This will subtract off the last digit.
number = number / 10;
console.log(number); // 546
We first use the modulo operator % to get the last digit, and we subtract it from number. That reduces the number from 5467 to 5460. Now to chop off the last digit (which is guaranteed to be a 0) we divide by 10 and get 546.
Written more concisely you could do:
number = (number - ( number % 10)) / 10;
There's a few things you can do the most concise being:
Math.floor(num / 10);
Or, convert to a string, remove the last character and convert back to number.
parseInt(num.toString().slice(0, -1));
If string representation would be fine for you then one other way is
var num = 5467,
cut = (num/10).toFixed(); // <-'547'
Well... warning..! i have to say toFixed() method rounds if necessary. So in this particular example it doesn't work.
I dont mind some of the other answers, but i feel that this maybe too fixed on it being a number.
Which it is, but you want to remove the last digit/char, regardless of the number, so why not substr?
http://www.w3schools.com/jsref/jsref_substr.asp
var s = 5467;
s = s.toString().substr(0, s.toString().length - 1);
console.log(s)
or even easier:
var s = (5467).toString();
s = s.substr(0, s.length - 1);
console.log(s)
These dont take into account single digit numbers, so passing in 1 would return blank. To answer that you could simply do a check like:
var s = (1).toString();
if(s.length > 1)
s = s.substr(0, s.length - 1);
console.log(s)
Also, similar question to:
Remove last digits from an int
Remove the last digits of a number (not string)
Removing the last digits in string
To truncate digits from the right hand side until the number is less than 30, keep dividing by 10 and rounding down until a suitable value is reached:
var n = 12341235;
while (n > 30) n = n/10|0;
document.write(n);
The greater than and division operations will coerce n to a number, so it can be a number or string. If ToNumber(n) results in NaN (e.g. n = 'foo'), then the value of n is not modified.
You can simply divide the number by 10 and parseInt()
var num = 5467;
num = parseInt(num/10);
Update :
To repeat the process until the answer is less than 30, use while loop as
var num = 5467;
while(num >= 30) {
num = parseInt(num/10);
}
document.write(num);

Prompting for a variable not working when trying to find random number

My assignment for Intro to Javascript is to: "Write a function that accepts two numbers and returns a random number between the two values." It seems easy enough until I attempt to prompt the variables to input, at which point the output seems incorrect.
This is code that is most recommended for finding a random number between two ints, in this case 1 and 6:
function getRandomizer(bottom, top) {
return function() {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
}
var rolldie = getRandomizer(1, 6);
document.write(rolldie());
The output I get is what it should be, a random integer between 1 and 6. However my assignment is to prompt for the numbers, which can be anything. So I do this, using 10 and 1 as example numbers:
var max = prompt("input 10");
var min = prompt("input 1");
function getRandomizer(bottom, top) {
return function() {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
}
var rolldie = getRandomizer(min, max);
document.write(rolldie());
The output: 961. Second try: 231. etc. If I set the variables max and min directly to 10 and 1, the code works perfectly, returning numbers between 1 and 10. But for some reason, prompting input and then entering the exact same numbers gives completely different output. Why does this happen and how can I fix it?
The reason this happens is that the prompts are being treated as strings. So you're actually getting numbers between 1101 and 1.
You can ensure the vars min and max are numbers by using parseInt:
var max = parseInt(prompt("input 10"));
Try this Code below:
var max = Number(prompt("input 10"));
var min = Number(prompt("input 1"));

Javascript Rounding to the nearest 2 decimal (however 5 rounds down)

All my values are being returned from the server as 3 decimal places. I need to round to the nearest 10, 2 decimal places, ex. decimal(18,2) from decimal(18,3). The catch is that when it's a 5, it needs to round down.
I need to do this in JavaScript :D
I can not guarantee 3 decimal places will be returned, that is the maximum.
ex. 4.494 -> 4.49
**ex. 4.495 -> 4.49**
ex. 4.496 -> 4.50
It seems you want special rounding only where the last digit is 5, so test for that and round those cases differently:
function myRound(n) {
// If ends in .nn5, round down
if (/\.\d\d5$/.test(''+n)) {
n = Math.floor(n*100)/100;
}
// Apply normal rounding
return n.toFixed(2);
}
console.log(myRound(4.494)); // 4.49
console.log(myRound(4.495)); // 4.49
console.log(myRound(4.496)); // 4.50
Perhaps create your own custom round function? check out Is it ok to overwrite the default Math.round javascript functionality?
Given the solution in the above post, you might modify it slightly like this:
Number.prototype.round = function(precision) {
var numPrecision = (!precision) ? 0 : parseInt(precision, 10);
var numBig = this * Math.pow(10, numPrecision);
var roundedNum;
if (numBig - Math.floor(numBig) == 0.5)
roundedNum = (Math.round(numBig) + 1) / Math.pow(10, numPrecision);
else
roundedNum = Math.round(numBig) / Math.pow(10, numPrecision);
return roundedNum;
};
var n = 2.344;
var x = n.round(2);
alert(x);

Math.round Rounding Error

I Want to round 1.006 to two decimals expecting 1.01 as output
When i did
var num = 1.006;
alert(Math.round(num,2)); //Outputs 1
alert(num.toFixed(2)); //Output 1.01
Similarly,
var num =1.106;
alert(Math.round(num,2)); //Outputs 1
alert(num.toFixed(2));; //Outputs 1.11
So
Is it safe to use toFixed() every time ?
Is toFixed() cross browser complaint?
Please suggest me.
P.S: I tried searching stack overflow for similar answers, but could not get proper answer.
EDIT:
Why does 1.015 return 1.01 where as 1.045 returns 1.05
var num =1.015;
alert(num.toFixed(2)); //Outputs 1.01
alert(Math.round(num*100)/100); //Outputs 1.01
Where as
var num = 1.045;
alert(num.toFixed(2)); //Outputs 1.04
alert(Math.round(num*100)/100); //Outputs 1.05
Try something like...
Math.round(num*100)/100
1) Multiple the original number by 10^x (10 to the power of x)
2) Apply Math.round() to the result
3) Divide result by 10^x
from: http://www.javascriptkit.com/javatutors/round.shtml
(to round any number to x decimal points)
This formula Math.round(num*100)/100 is not always good. Example
Math.round(0.145*100)/100 = 0.14
this is wrong, we want it to be 0.15
Explanation
The problem is that we have floats like that
0.145 * 100 = 14.499999999999998
step one
so If we round, we need to add a little bit to our product.
0.145 * 100 + 1e-14 = 14.500000000000009
I assume that sometimes the product might be something like 1.000000000000001, but it would not be a problem if we add to it, right?
step two
Calculate how much should we add?
We know float in java script is 17 digits.
let num = 0.145
let a = Math.round(num*100)/100
let b = a.toString().length
let c = 17-b-2
let result = Math.round(num*100 + 0.1**c)/100
console.log(result)
console.log('not - ' + a )
(-2) - is just to be sure we are not falling into the same trap of rounding.
One-liner:
let num = 0.145
let result = Math.round(num*100 + 0.1**(17-2-(Math.round(num*100)/100).toString().length))/100
Extras
Remember, that everything above is true for positive numbers. If you rounding negative number you would need to subtract a little bit. So the very final One-liner would be:
let num = -0.145
let result = Math.round(num*100 + Math.sign(num)*0.1**(17-2-(Math.round(num*100)/100).toString().length))/100
I realize this problem is rather old, but I keep running into it even 5 years after the question has been asked.
A working solution to this rounding problem I know of is to convert the number to a string, get the required precision number and round up or down using math rules.
An example where Math.round provides unexpected rounding and an example of string rounding can be found in the following fiddle:
http://jsfiddle.net/Shinigami84/vwx1yjnr/
function round(number, decimals = 0) {
let strNum = '' + number;
let negCoef = number < 0 ? -1 : 1;
let dotIndex = strNum.indexOf('.');
let start = dotIndex + decimals + 1;
let dec = Number.parseInt(strNum.substring(start, start + 1));
let remainder = dec >= 5 ? 1 / Math.pow(10, decimals) : 0;
let result = Number.parseFloat(strNum.substring(0, start)) + remainder * negCoef;
return result.toFixed(decimals);
}
let num = 0.145;
let precision = 2;
console.log('math round', Math.round(num*Math.pow(10, precision))/Math.pow(10, precision));
// 0.145 rounded down to 0.14 - unexpected result
console.log('string round', round(num, precision));
// 0.145 rounded up to 0.15 - expected result
Math.round doesn't work properly here because 0.145 multiplied by 100 is 14.499999999999998, not 14.5. Thus, Math.round will round it down as if it was 14.4. If you convert it to a string and subtract required digit (5), then round it using standard math rules, you will get an expected result of 0.15 (actually, 0.14 + 0.01 = 0.15000000000000002, use "toFixed" to get a nice, round result).

Categories