How to round decimal value up to nearest 0.05 value?
How about: Math.Ceiling(myValue * 20) / 20
How about: Math.Ceiling(myValue * 20) / 20
Just to make sure we’re on the same page, G is the most significant bit of the three, R comes next and S can be thought of as the least significant bit because its value partially represents the even less significant bits that have been truncated in the calculations. These three bits are only used … Read more
Not sure why format would be so inconsistent but from memory the spec for it is…complex. Meantime, you can use the round function (ref). Which is less than perfect, but is functional. If you need to have a particular number of sig figs you can use THE POWER OF MATHS! and do something like: <xsl:value-of … Read more
Do floating point division then convert to an int. No extra modules needed. Python 3: >>> int(-1 / 2) 0 >>> int(-3 / 2) -1 >>> int(1 / 2) 0 >>> int(3 / 2) 1 Python 2: >>> int(float(-1) / 2) 0 >>> int(float(-3) / 2) -1 >>> int(float(1) / 2) 0 >>> int(float(3) / … Read more
>>> parseFloat(0.9999999.toFixed(4)); 1 >>> parseFloat(0.0009999999.toFixed(4)); 0.001 >>> parseFloat(0.0000009999999.toFixed(4)); 0
int(x) Conversion to integer will truncate (towards 0.0), like math.trunc. For non-negative numbers, this is downward. If your number can be negative, this will round the magnitude downward, unlike math.floor which rounds towards -Infinity, making a lower value. (Less positive or more negative). Python integers are arbitrary precision, so even very large floats can be … Read more
Using BigDecimal without any doubles (improved on the answer from marcolopes): public static BigDecimal round(BigDecimal value, BigDecimal increment, RoundingMode roundingMode) { if (increment.signum() == 0) { // 0 increment does not make much sense, but prevent division by 0 return value; } else { BigDecimal divided = value.divide(increment, 0, roundingMode); BigDecimal result = divided.multiply(increment); return … Read more
The first step would be to define the colors you want to compare to. The second step is to find the smallest distance from your color to one of the colors you chose in the previous step. In order to be able to measure that distance you need an Euclidian space in which to model … Read more
It looks like this was a long-standing bug in JDK 7 that was finally fixed. See for example: https://bugs.openjdk.java.net/browse/JDK-8029896 https://bugs.openjdk.java.net/browse/JDK-7131459 There is a draft plan to provide the following advisory with JDK 8 which explains the issue: ——————————————————————— Area: Core Libraries / java.text Synopsis: A wrong rounding behavior of JDK7 has been fixed. The rounding … Read more
Multiply it by four, round it as you need to an integer, then divide it by four again: x = Math.Round (x * 4, MidpointRounding.ToEven) / 4; The various options for rounding, and their explanations, can be found in this excellent answer here 🙂