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 /
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 /
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/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/n7ecyr7h/1/
Share Improve this question edited Oct 13, 2014 at 7:54 AddyProg asked Oct 13, 2014 at 7:43 AddyProgAddyProg 3,06014 gold badges63 silver badges115 bronze badges 5-
1
The output should be -12. It is the value of
-12.369754
rounded up (that is, to the nearest whole number towards positive infinity). BTW what on Earth is thatdecimalPoint
variable and the conditional that makes no sense at all? – Powerslave Commented Oct 13, 2014 at 7:46 - Math.ceil(-12.369754) is -12. Please check references at MDN. – Alberto De Caro Commented Oct 13, 2014 at 7:49
- @Powerslave: check the Question, I have explained it – AddyProg Commented Oct 13, 2014 at 7:54
-
1. Why are you using parseFloat on a float ? That makes no sense. 2. Are you seriously testing each possible value of
decimalPoints
instead of simply doing* Math.pow(10, decimalPoints)
? – Sebastien C. Commented Oct 13, 2014 at 8:43 -
@AdilWaqar Well, you gave an example only, not an explanation. And that example could be faulty (you might have been misunderstanding the concept of rounding up) as well since you're not rounding up, but away from zero.
Math.ceil
works as intended. It is just you wanted to do something pletely different. – Powerslave Commented Oct 13, 2014 at 12:13
2 Answers
Reset to default 4The 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/Guffa/n7ecyr7h/3/
Math.ceil(-1.1234) will be -1.12 because in negative -1.12 > -1.1234. I think you misunderstood mathematically.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744903879a4600136.html
评论列表(0条)