round double to 0.5
x = 13000 / 9000.0;
denominator = 2;
a.text = String(round(x*denominator )/denominator );
First convert 1.444 to 2.888, then round to 3.0 and then divide by 2 to get 1.5. In this case, the denominator of 0.5 is 2 (i.e. 1/2). If you want to round to nearest quarter (0.25,0.5, 0.75, 0.00), then denominator=4
I Should point out that this works perfectly if the denominator is a power of 2. If it is not, say denominator=3
, then you can get weird answers like 1.99999999 instead of 2 for particular values.
How do I round to the nearest 0.5?
Multiply your rating by 2, then round using Math.Round(rating, MidpointRounding.AwayFromZero)
, then divide that value by 2.
Math.Round(value * 2, MidpointRounding.AwayFromZero) / 2
Java round to nearest .5
@SamiKorhonen said this in a comment:
Multiply by two, round and finally divide by two
So this is that code:
public static double roundToHalf(double d) {
return Math.round(d * 2) / 2.0;
}
public static void main(String[] args) {
double d1 = roundToHalf(1.1);
double d2 = roundToHalf(1.3);
double d3 = roundToHalf(2.5);
double d4 = roundToHalf(3.223920);
double d5 = roundToHalf(3);
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
System.out.println(d4);
System.out.println(d5);
}
Output:
1.0
1.5
2.5
3.0
3.0
Round double to number in interval, defining by step
Java has instruments which can round numbers to n decimal places, see How to round a number to n decimal places in Java. For rounding to any interval you specified, you may have to manually use Math.round
.
Formula:
Given an interval r
and a double value x
to round, a simple formula is:
x_rounded = Math.round(x/r)*r;
Examples:
double x = 0.59999;
double r = 0.25; // Quarters
x = Math.round(x/r)*r;
System.out.println(x); // Result is 0.5
double x = 0.59999;
double r = 0.1; // Tenths
x = Math.round(x/r)*r;
System.out.println(x); // Result is approximately 0.6
double x = 0.31421;
double r = 0.125; // Eighths
x = Math.round(x/r)*r;
System.out.println(x); // Result is exactly 0.375
Proof:
- The interval
r
can be thought as the value of a fractional unit.- When
r = 0.25
, the fractional unit is a quarter.
- When
- The value
x/r
represents the number of fractional units that make upx
.- When
x = 0.75
,r = 0.25
,x/r == 3
, becausex
contains three fractional unit, which is the quarter.x/r
represents the number of quarters.
- When
Math.round(x)
roundsx
to the nearest integral value. Similarly,Math.round(x/r)
roundsx/r
to the nearest integral multiple of that fraction.- For
x = 0.7, r = 0.25
, we havex/r = 2.8
, representing 2.8 quarters.Math.round(x/r)
therefore rounds the value to the nearest quarter, 3 quarters.
- For
Math.round(x/r)*r
therefore roundsx
to the nearest fractional intervalr
. The multiplier is needed becauser
is the value of each fractional unit.- For
x = 0.7, r = 0.25
,Math.round(x/r)
represents 3 quarters. It has to be multiplied byr=0.25
to get the rounded value ofx
.
- For
Rounding down a float to nearest 0.5 in Objective C / Cocoa Touch
floor()
will round down floating point numbers to the next integer. If you first multiply by 2, round down, then divide by two, you will get the desired result.
float rounded = value < 0.5f ? 0.5f : floorf(value * 2) / 2;
How to round up and down double to the nearest interval in dart/flutter
- Multiply your number by two before any calculations.
- Use the desired round feature. (Floor or Ceiling).
- Divide the result by two again:
double a = 134.78;
print((2*a).floorToDouble()/2); // prints 134.5
print((2*a).ceilToDouble()/2);// prints 135
Which is the desired result.
You can read more here.
Rounding of nearest 0.5
If it is required for 13.1, round to 13.5
and 13.9, round to 14.0
, then:
double a = 13.1;
double rounded = Math.Ceil(a * 2) / 2;
How can I round to next floor 0.5 in Kotlin?
Multiply by 2, take the floor
, then divide by 2 again.
There doesn't seem to be a built-in way, but you can always write it yourself:
fun Double.roundDownToMultipleOf(base: Double): Double = base * floor(this / base)
Use as 12.56.roundDownToMultipleOf(0.5)
.
Related Topics
How to Copy Skspritenode with Skphysicsbody
How to Create a Nsmutabledictionary in Swift
Get Color of Point in a Skscene Swift
Realitykit - How to Edit or Add a Lighting
Swift "Is" Operator with Type Stored in Variable
Different Colors for Bars in Barchart Depend on Value
Symbol Is Considered to Be an Identifier, Not an Operator
Swift Protocol Defining Class Method Returning Self
Changing Nav Bar Item Programmatically in Swift
Avplayer Can't Resume After Paused + Some Waiting
Properly Using Firebase Cloud Functions and Stripe
Swift: Copy Information Selected by User in Abpersonviewcontroller to Dictionary
Swift Protocol for String Interpolation
How to Do a Long Press in Swift
Nsurlsessiondatadelegate Method Didreceivedata and Others Are Not Called
Swift/Scenekit Problems Getting Touch Events from Scnscene and Overlayskscene