Round up from .5
This is not my own function, and unfortunately, I can't find where I got it at the moment (originally found as an anonymous comment at the Statistically Significant blog), but it should help with what you need.
round2 = function(x, digits) {
posneg = sign(x)
z = abs(x)*10^digits
z = z + 0.5 + sqrt(.Machine$double.eps)
z = trunc(z)
z = z/10^digits
z*posneg
}
x
is the object you want to round, and digits
is the number of digits you are rounding to.
An Example
x = c(1.85, 1.54, 1.65, 1.85, 1.84)
round(x, 1)
# [1] 1.8 1.5 1.6 1.8 1.8
round2(x, 1)
# [1] 1.9 1.5 1.7 1.9 1.8
(Thanks @Gregor for the addition of + sqrt(.Machine$double.eps)
.)
Round up from .5 in a %
Math.Round()
uses banker's rounding by default, so it will round to the nearest even number when dealing with [number].5
. In other words, Math.Round(0.5)
is 0, while Math.Round(1.5)
is 2. You can make it always round up by saying Math.Round(0.5, MidpointRounding.AwayFromZero);
How to round up a number to the nearest .5?
CEILING(number, significance)
- Number - The value you want to round.
- Significance -The multiple to which you want to round.
Returns number rounded up, away from zero, to the nearest multiple of significance. For example, if you want to avoid using pennies in your prices and your product is priced at
$4.42
, use the formula=CEILING(4.42,0.05)
to round prices up to the nearest nickel.
So, in your case:
CEILING(value, 0.5)
More info on Microsoft support
R: round up from .6
I'm not sure if this is a duplicate of the post linked to in the comments (but the post may certainly be relevant). From what I understand OP would like to "round" values up or down if they are >= 0.6
or < 0.6
, respectively. (The linked post refers to the number of digits a number should be rounded to, which is a different issue.)
In response to OPs question, here is an option where we define a custom function my.round
my.round <- function(x, digits = 4, val = 0.6) {
z <- x * 10^digits
z <- ifelse(signif(z - trunc(z), 1) >= val, trunc(z + 1), trunc(z))
z / 10^digits
}
Then
x <- 53.51245
my.round(x, 4)
#[1] 53.5124
x <- 53.51246
my.round(x, 4)
#[1] 53.5125
my.round
is vectorised, so we could have done
my.round(c(53.51245, 53.51246, 53.51246789), digits = 4)
#[1] 53.5124 53.5125 53.5125
How to round up to closest .5 using C#
I've taken the liberty of assuming that negative values are a valid input and that the desired behavior is to always round away from zero; all other valid answers will round towards zero in case of negative inputs.
The implementation is ugly but effective and easy to understand:
public static double Round(double d)
{
var absoluteValue = Math.Abs(d);
var integralPart = (long)absoluteValue;
var decimalPart = absoluteValue - integralPart;
var sign = Math.Sign(d);
double roundedNumber;
if (decimalPart > 0.5)
{
roundedNumber = integralPart + 1;
}
else if (decimalPart == 0)
{
roundedNumber = absoluteValue;
}
else
{
roundedNumber = integralPart + 0.5;
}
return sign * roundedNumber;
}
How to always round up a XX.5 in numpy
import numpy as np
A = [ [1.0, 1.5, 3.0], [2.5, 13.4, 4.1], [13.4, 41.3, 5.1]]
A = np.array(A)
print(A)
def rounder(x):
if (x-int(x) >= 0.5):
return np.ceil(x)
else:
return np.floor(x)
rounder_vec = np.vectorize(rounder)
whole = rounder_vec(A)
print(whole)
Alternatively, you can also look at numpy.ceil, numpy.floor, numpy.trunc for other rounding styles
Round up from 0.5 AND round up from -0.5 in R
The usual way to get everything to round 0.5 up is to use floor(x + 0.5)
:
x <- c(-0.7, -0.5, 0, 0.2, 0.5)
floor(x + 0.5)
#> [1] -1 0 0 0 1
Created on 2021-12-23 by the reprex package (v2.0.1)
Round function does not work well when 5 is the first non-decimal digit
There's a possible work-around for the round function's behaviour.
import math
some_value = 35
print(math.ceil(someValue / 10) * 10 if some_value % 5 == 0 else round(some_value, -1))
This code checks if the value is divisible by 5, and if it is, it divides the value by 10, rounds UP the value (ceil
), and again multiplies it by 10 - otherwise executes the simple old round function :)
You could wrap that in a function as well :-
def round_val(val):
return math.ceil(val / 10) * 10 if val % 5 == 0 else round(val, -1)
Related Topics
Select the N Most Frequent Values in a Variable
Find All Combinations of a Set of Numbers That Add Up to a Certain Total
R: Error in Usemethod("Tbl_Vars")
How to Dplyr Rename a Column, by Column Index
Delete Rows Containing Specific Strings in R
Creating Grouped Bar-Plot of Multi-Column Data in R
How to Convert a Data Frame Column to Numeric Type
Break Dataframe into Smaller Dataframe'S and Save Them
How to Replace Negative Values in a Dataframe Column With a Different Value
Conditionally Remove Rows from a Database Using R
Adding Some Space Between the X-Axis and the Bars, in Ggplot
Convert Categorical Variables to Numeric in R
How to Select Variables in an R Dataframe Whose Names Contain a Particular String
Calculate Difference Between Values in Consecutive Rows by Group
How to Loop Through List and Create Separate Dataframes in R
Rstudio Suddenly Stopped Showing Plots in the Plot Pane
How to Remove Na from a Factor Variable (And from a Ggplot Chart)