round of this code not use any function ,loop and math function javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

round of this code not use any function ,loop and math function javascript?

var arr = 440.4;

3rd Aug 2023, 4:23 PM
Suresh Prajapati
Suresh Prajapati - avatar
4 Answers
+ 1
Mirielle ~~ is not exactly round, more like Math.trunc(). Interesting, but I can't find a shortcut equivalent to Math.round. https://code.sololearn.com/WfMgCx17ytBg/?ref=app
4th Aug 2023, 2:02 PM
Bob_Li
Bob_Li - avatar
0
Mirielle For negative numbers, ~~ gives different result from floor. But you're right. If it works, and it is not for something critical, over-analyzing is not necessary. The rounding rule to round up or keep the same if digit is odd or even number before 5 makes it rounding what it is. no way around that.
4th Aug 2023, 2:17 PM
Bob_Li
Bob_Li - avatar
0
Mirielle I was looking at Math.round and I felt that "add if odd is followed by 5" rule was not implemented for negative values. It seems like a design choice. Rounding, floor, ceil are really fiddly topics. https://en.wikipedia.org/wiki/Rounding#
4th Aug 2023, 3:06 PM
Bob_Li
Bob_Li - avatar
0
arr - arr % 1
5th Aug 2023, 11:53 AM
R-G-B
R-G-B - avatar