Round to nearest 0.05 using Python
def round_to(n, precision): correction = 0.5 if n >= 0 else -0.5 return int( n/precision+correction ) * precision def round_to_05(n): return round_to(n, 0.05)
def round_to(n, precision): correction = 0.5 if n >= 0 else -0.5 return int( n/precision+correction ) * precision def round_to_05(n): return round_to(n, 0.05)
Math.round(num * 10) / 10 works, and here is an example… var number = 12.3456789 var rounded = Math.round(number * 10) / 10 // rounded is 12.3 If you want it to have one decimal place, even when that would be a 0, then add… var fixed = rounded.toFixed(1) // ‘fixed’ is always to one … Read more
(Math.round((16.185*Math.pow(10,2)).toFixed(1))/Math.pow(10,2)).toFixed(2); If your value is, for example 16.199 normal round will return 16.2… but with this method youll get last 0 too, so you see 16.20! But keep in mind that the value will returned as string. If you want to use it for further operations, you have to parsefloat it 🙂 And now as … Read more
Why 1/3 as a double is 0.33333333333333331 The closest way to represent 1/3 in binary is like this: 0.0101010101… That’s the same as the series 1/4 + (1/4)^2 + (1/4)^3 + (1/4)^4… Of course, this is limited by the number of bits you can store in a double. A double is 64 bits, but one … Read more
This works in general: ceil(number*10)/10 So in Ruby it should be like: (number*10).ceil/10.0
Datetime is only accurate to 3ms. Therefore it’ll round to the nearest multiple of 3ms. To overcome this, look at the datetime2. Note that this is for SQL2008+ only EDIT: it’s not quite only to 3ms. It’s rounded to increments of of .000, .003, or .007 seconds
By default the .ToString() method of Double returns 15 digits of precision. If you want the full 17 digits that the double value holds internally, you need to pass the “G17” format specifier to the method. String s = value.ToString(“G17”); Sourced from the MSDN docs: By default, the return value only contains 15 digits of … Read more
The issue you’re having between -0. and +0. is part of the specification of how floats are supposed to behave (IEEE754). In some circumstance, one needs this distinction. See, for example, the docs that are linked to in the docs for around. It’s also worth noting that the two zeros should compare to equal, so … Read more