It should be F2 according to MSDN Standard numeric Formatting

python,string,formatting,rounding

Well, you could always round the return statement. So, round(payment, 2) for example. And, I am not sure why you are using \r. (Could you tell me why?). You could do %.2f to use two decimal places instead.

php,rounding,number-formatting

First do an equation, than round and as the last one thing use number_format. echo number_format(round($vehicle->prices['total_rental'] * .5, 2), 2, '.', ','); ...

In the absence of a concrete demonstration, I shall point out the two likely culprits: "We normally should round up" is absolutely false. There are many different rounding system. sprintf, for example, uses round-to-even. $ perl -E'say sprintf "%1\$.1f => %1\$.0f", $_ for 3.5, 4.5, 5.5, 6.5' 3.5 => 4...

You can use the round() function (which rounds a floating point number to the nearest integral value) and apply a "scale factor" of 10: func roundToTens(x : Double) -> Int { return 10 * Int(round(x / 10.0)) } Example usage: println(roundToTens(4.9)) // 0 println(roundToTens(15.1)) // 20 In the second example,...

You can explicitly pass the value to the function by defining the FUN=function() value. Using the mtcars dataset library(pastecs) Note that the first line is an abbreviated version of the second by(mtcars$mpg, mtcars$am, stat.desc, norm = TRUE, basic = TRUE) by(mtcars$mpg, mtcars$am, function(X) stat.desc(X, norm = TRUE, basic = TRUE))...

Today I found an mathematically correct answer: public static Real Round(Real r, int precision) { Real scaled = Real.Pow(10, precision + 1); Real multiplied = r*scaled; Real truncated = Trunc(multiplied); Real lastNumber = truncated - Trunc(truncated/10)*10; if (lastNumber >= 5) { truncated += 10; } truncated = Trunc(truncated/10); return truncated...

One possible way: public static long symmetricRound( double d ) { return d < 0 ? - Math.round( -d ) : Math.round( d ); } If the number is negative, round its positive value, then negate the result. If it's positive or zero, just use Math.round() as is....

Your computer can't represent most decimal fractions numbers exactly, since it works with binary numbers. Instead, it tries to find the closest number it can represent, and use that instead. For instance, my computer represents the following numbers as: decimal actual representation 0.115 0.115000002 0.225 0.224999994 0.245 0.245000005 0.335 0.335000008...

It is useful when you are performing multiple rounding operations and want the cumulative result to be a true average, and not skewed up or down, as it would be with HALF_UP or HALF_DOWN. Specifically, it is useful for statistical analysis (you don't want the results polluted by a non-random...

I'm not certain this is actually technically a bug. The issue is that when you write 4.45 and the value is interpreted as a double, that is rounded to a binary fraction: a sum of multiples of powers of 2, to some precision. (That rounding itself is done with HALF_EVEN...

Why should it be 0? 1.67460317460317 + -1.6746 ------------------- = 0.00000317460317 = 3.17e-06 and the difference between your 3.21 and the "real" math 3.17 is just floating point inaccuracy....

java,php,mysql,datetime,rounding

Here is an answer from the following stackoverflow post : How to round a time to the nearest 15 minute segment SELECT FROM_UNIXTIME( TRUNCATE(UNIX_TIMESTAMP(NOW()) / 900,0)*900); Credits to Gavin Towey! Just replace 900 (15 mins) with 600 (10 mins)....

Math.floor(x+0.7) should do it. This should work for an arbitrary mantissa. Just add the offset to the next integer to your value and round down. The rounding is done by floor. Here is what the java API says to floor: Returns the largest (closest to positive infinity) double value that...

c++,matlab,math,floating-point,rounding

The original publication that introduced Tuckerman rounding for the square root was: Ramesh C. Agarwal, James W. Cooley, Fred G. Gustavson, James B. Shearer, Gordon Slishman, Bryant Tuckerman, "New scalar and vector elementary functions for the IBM System/370", IBM J. Res. Develop., Vol. 30, No. 2, March 1986, pp. 126-144....

python,c++,python-2.7,rounding

You can use double rounding to overcome the inability of binary arithmetic to exactly represent a decimal value. round(round(1.265, 3) + 0.0005, 2) ...

javascript,arrays,razor,rounding,asp.net-mvc-viewmodel

Float values passed to JavaScript as JSON or direct values must have "." as decimal separator. Some cultures use "," and thus parsing of such numbers as JSON or with parseFloat will fail to recognize decimal part. Fix: use invariant culture to format numbers or use existing libraries (like Json.Net)...

php,rounding,precision,floating-point-precision

There is no need to replace round with number_format. You could use round with the flag to round down. Choose what is best for the application. Are you rounding a float? Use round Are you wanting to format a number and group by thousands? Use number_format <?php $number = '0.5600';...

java,floating-point,double,rounding,rounding-error

If you know what precision you want you can round it to say 6 decimal places. eg. public static double round6(double x) { return Math.round(x * 1e6) / 1e6; } round6(0.7f) == 0.7 The fact the x passed in was a float doesn't matter provided this precision is suitable....

java,string,math,rounding,number-formatting

use BigDecimal. 1 java doc BigDecimal bd = new BigDecimal("String") ...

round will take care of the precision if that digit is not 0 as the last 0 has no effect in any mathematical operation. If the number is 1.569 then round will return 1.57 If you want that 0 the you can use - number_format(58.900662, 2, '.', ','); number_format()...

In your code when you are changing rounding methods, you are passing already rounded value to next step. For example, value was rounded by HALF_EVEN to two places and then this already rounded value (2 places) is being passed to FLOOR step and so on. To get behavior you are...

You can use floor() to round down: $number = 1.399; $roundedNumber = number_format(floor($number * 100)/100, 2); echo $roundedNumber; //Returns 1.39 ...

Use arithmetic for this: ORDER BY CEIL(price/10) asc, . . . If you want to see the price rounded up: SELECT CEIL(price/10)*10 EDIT: Round the price down: ORDER BY FLOOR(price/10) asc, views desc, dateadded asc, liked desc, size desc ...

math.ceil rounds up. import math eaters = input("How many people are attending the party?") pieces = input("How many pieces will everyone eat on average?") pizzas = float(eaters) * float(pieces) orders_needed = math.ceil(pizzas/8) print(orders_needed) ...

Floating-point numbers don't have decimal places. They have binary places, and the two are not commensurable. Any attempt to modify a floating-point variable to have a specific number of decimal places is doomed to failure. You have to do the rounding to a specified number of decimal places after conversion...

awk could do it easily: awk '{printf "%.2f%s",$3,!(NR%2)?"\n":FS}' file with your example: kent$ cat f xmin = 0 xmax = 15.393970521541949 xmin = 15.393970521541949 xmax = 27.58997052154195 xmin = 27.58997052154195 xmax = 46.05797052154195 xmin = 46.05797052154195 xmax = 65.67797052154194 xmin = 65.67797052154194 xmax = 81.08048969617173 xmin = 81.08048969617173 xmax =...

java,casting,int,double,rounding

In all the outputs you printed (except the first one which is 0) Speed is smaller than 1 (7.026718463748694E-4, 5.27003884781152E-4, etc...). Notice the negative exponent. Therefore it's no wonder ceil returns 1....

... learn math: $val = intval($var / 50)*50+50; ...

Some code to Andrew's correct answer: var settings = new JsonSerializerSettings(); settings.FloatParseHandling = FloatParseHandling.Decimal; string json = @"{ ""rings"" : [ -9221300.3411999997, 4120326.8838 ] }"; var rss = JsonConvert.DeserializeObject(json, settings); ...

c#,visual-studio,radio-button,rounding,shapes

I think this could be your problem. In your method: private void trackBar1_Scroll(object sender, EventArgs e) You are declaring new variables called Area and Length and not using the class variables which you are using when rounding the values. Try write your calculations without the type before Area and Length....

javascript,floating-point,rounding,floor

function formatNumber(x) { // convert it to a string var s = "" + x; // if x is integer, the point is missing, so add it if (s.indexOf(".") == -1) { s += "."; } // make sure if we have at least 2 decimals s += "00";...

excel-formula,rounding,worksheet-function

It's worth noting that Excel has built-in functions for working with multiples: CEILING and FLOOR (in newer versions you have CEILING.MATH and FLOOR.MATH). In your case, this should work: =CEILING(A1,20)-A1 ...

function myround($a, $b) { $d = $a * $b / 100.0; $f = floor($d); if ((rand() / getrandmax()) > ($d - $f)) return $f + 1.0; return $f; } ...

From your plunker I see you are mixing versions: From https://github.com/aguirrel/ng-currency#versions Versions If you use angular 1.2.x please, use 0.7.x version. If you use angular 1.3.x or above just use 0.8.x version instead. Please, use https://rawgit.com/aguirrel/ng-currency/v0.8.x/src/ng-currency.js instead of uploaded ng-currency.js in you plunkr. <script src="https://rawgit.com/aguirrel/ng-currency/v0.8.x/src/ng-currency.js"></script> You can see it in...

Something like: timeval to_timeval(double usec) { return { usec / 1000000, usec % 1000000 }; } ...

sql,sql-server-2008,batch-file,rounding

Operator / is type-specific, so you should cast your integer to the float, like this: ROUND(CAST(($Var3) AS float) / 6000), 2) or use the division by float, like this: ROUND(($Var3 / 6000.0), 2) Returns the data type of the argument with the higher precedence. For more information, see Data Type...

I'm assuming that by float you mean a 32-bit IEEE-754 binary floating point value, by double you mean a 64-bit IEEE-754 binary floating point value, and by int you mean a 32-bit integer. Why does this happen? The range of float far exceeds that of int Yes, but the precision...

The e is the exponential symbol in scientific notation, you get it in toString() when the number is really big (or small). For example 0.0000000000001 yields 1e-13. As for the other part of your question: The method is applicable also when there is no exponential part. In this case the...

java,java-8,rounding,number-rounding

Seems that it's intended change. The JDK 1.7 behavior was incorrect. The problem is that you simply cannot represent the number 10.55555 using the double type. It stores the data in IEEE binary format, so when you assign the decimal 10.55555 number to the double variable, you actually get the...

You should have a look at how floats are handled, and what limitations they have. https://docs.python.org/3/tutorial/floatingpoint.html is a Python specific explanation. In your particular example, Python chooses to use a representation that has a limited number of digits (depends on the Python version). Internally, different float values may share the...

I would test the distance with the standard rounding, then adjust and round again : for (String string : new String[] {"1.1245", "1.1244", "1.1235", "1.1249", "1.1299"}) { BigDecimal a = new BigDecimal(string); if (a.setScale(2, BigDecimal.ROUND_HALF_UP).subtract(a).compareTo(new BigDecimal("-0.0044")) < 0) { System.out.println(string + ":" + a.add(new BigDecimal("0.001")).setScale(2, BigDecimal.ROUND_HALF_UP)); } else { System.out.println(string...

matlab,ms-access,rounding,numeric

Rounding a trailing 0 (zero) is not rounding. What you miss is the format that displays the number. Where you do this, set number of decimals to two....

python,decimal,rounding,currency,babel

If you follow the code through to apply you can see a reference to a bankersround procedure, which tells us what we need to know. Babel is using the bankers round method of rounding, which rounds to the nearest even number on 5. So, .245 and .235 round to .24...

If you overwrite the standard format with #.00 you have no grouping seperator in your format. For your expected case you have to include the grouping seperator again into your custom format: DecimalFormat theFormatter = new DecimalFormat("#,###.00", theSymbols); The pattern definition symbols can be found in the Doc...

python,templates,flask,rounding,jinja2

You can use string filter, then use str.rstrip: >>> import jinja2 >>> print(jinja2.Template(''' ... {{ (1.55555|round(2)|string).rstrip('.0') }} ... {{ (1.5|round(2)|string).rstrip('.0') }} ... {{ (1.0|round(2)|string).rstrip('.0') }} ... {{ (1|round(2)|string).rstrip('.0') }} ... ''').render()) 1.56 1.5 1 1 NOTE Using str.rstrip, you will get an empty string for 0. >>> jinja2.Template('''{{ (0|round(2)|string()).strip('.0') }}''').render()...

number_format() should do the trick. // english notation without thousands separator $amount2 = number_format($amount2, 2, '.', ''); // 1234.57 ...

A most serious mistake. I asked the same question on the Intel developer forums and andysem corrected me, pointing out the behavior is to round to the nearest integer. I was mistaken into thinking it was biased because the formula from MSDN, https://msdn.microsoft.com/en-us/library/bb513995.aspx was (x * y + 16384) >>...

I've resolved the issue using the following code: Private Sub Command1_Click() roundnumber = Val(Text1.Text) Text1.Text = Round(roundnumber, 2) Text1.Text = AsymUp(Val(Text1.Text), 10) End Sub Function AsymUp(ByVal X As Double, _ Optional ByVal Factor As Double = 1) As Double Dim Temp As Double Temp = Int(X * Factor) AsymUp =...

python,logic,rounding,number-rounding

This is a weird restriction, but you could do this: x = str(x) dec_index = x.find('.') tenth_index = dec_index + 1 tenth_place = x[tenth_index] # will be a string of length 1 should_round_up = 5 + tenth_place.find('5') + tenth_place.find('6') + tenth_place.find('7') + tenth_place.find('8') + tenth_place.find('9') print int(x[0:dec_index]) + should_round_up What...

This code will do the job. double number = 1551; if (number >= 0 && number <= 100) { number = Math.Round(number); } else if (number > 100 && number <= 10000) { number = Math.Round(number / 100) * 100; } else if (number > 10000) { number = Math.Round(number...

c++,geometry,rounding,cgal,convex

In the end I discovered the root of this problem was the fact that the convex hull contained lots of triangles, whereas my input shapes were often cube-shaped, making each quadrilateral region appear as 2 triangles which had extremely similar plane equations, causing some sort of problem in the algorithm...

This is because float is based on floating point notation. In rude words it tries to represent your decimal number as a sum of fractions of power 2. It means it will try to sum 1/2^n1 + 1/2^n2 + 1/2^n3 .... 1/2^nm until gets closes or exact value that you...

You probably want signif which rounds off to a given number of significant digits. x <- runif(16) * 10^(7*runif(16)-5) cbind(x, mx = my.func(x), sx = signif(x, 3)) x mx sx [1,] 1.395044e-01 1.40e-01 1.40e-01 [2,] 9.751368e-06 9.80e-06 9.75e-06 [3,] 3.451619e-04 3.50e-04 3.45e-04 [4,] 2.203204e-03 2.20e-03 2.20e-03 [5,] 6.660684e-05 6.70e-05 6.66e-05...

trunc function will truncate number to given number of decimals: select trunc(2.7, 0); trunc ------- 2 (1 row) ...

geom_boxplot calls boxplot.stats to calculate the positions of the upper and lower whiskers. You can do it too: > boxplot.stats(v) $stats [1] 93.340 96.069 97.876 99.087 100.359 $n [1] 24 $conf [1] 96.90265 98.84935 $out [1] -234.347 75.764 (v is assumed to be your input data vector): From the boxplot.stats...

There was a bug in Java 8 regarding the NumberFormat and RoundingMod HALF_UP see 8039915. This was fixed with 8u40 (Release Notes).

javascript,python,bit-manipulation,rounding,floor

just cast the float to an int int(2.667) it will always floor/truncate the float once the float is non negative if you have negative numbers you do want to floor not just truncate use math.floor . In [7]: int(2.667 ) Out[7]: 2 In [22]: from math import floor In [23]:...

double,cross-platform,rounding,precision,deterministic

I've actually been thinking about the whole matter in the wrong way. Instead of trying to minimize errors with greater accuracy (and more overhead), I should just use a type that stores and operates deterministically. I used the answer here: Fixed point math in c#? which helped me create a...

The best way I know is using forward-pipe operator %>% from magrittr package > library(magrittr) > mean(c(0.34333, 0.1728, 0.5789)) [1] 0.36501 > mean(c(0.34333, 0.1728, 0.5789)) %>% round(3) [1] 0.365 I should also mention, that package magrittr is used by another very popular package dplyr, which provides additional functionality for data...

The second parameter (-1 in your example) specifies to the ROUND function how many digits to use on the approximation. 0 would approximate mathematically to the closest integer. A positive number (n) would approximate using the most n significant digits - ie. ROUND(1.23456, 3) = 1,235 A negative number (-n)...

public static int Round(double number) { int numberLenght = number.ToString().Length; int times = (int) Math.Pow(10, numberLenght-2); return (int) (number*times); } You something like this?...

excel,if-statement,excel-formula,rounding

MANY THANKS to JNevill =IF(B4>0,MROUND((((B4-B5)/2)+B5)+B7+IF(E8>0,E8)+IF(B8="yes",B7)+B9,.0625)," ") https://support.office.com/en-ie/article/MROUND-function-86dc90eb-1e0e-4e03-8913-6debe3f18c5f However, this only rounded to the nearest (which is what I asked for) and not always up . I believe =CEILING() is more of what I was needing for because it is rounding to the next highest 1/16. =IF(B4>0,CEILING((((B4-B5)/2)+B5)+B7+IF(E8>0,E8)+IF(B8="yes",B7)+B9,0.0625)," ")...

If you are working with decimal, the default is DECIMAL(10, 0), you should specify the precision and scale, for example: DECIMAL(10, 4) The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.7 are as follows: M is the maximum number of...

java,int,double,rounding,mouselistener

Java is doing some hidden casts. Depending on which variable is a double or an int, you might get unexpected results because some double values have been casted to ints. I would make sure all your variables are doubles. Also, do not forget that floating points also have representation limitations,...

python,mysql,decimal,rounding,mysql-python

The number column in the Testing database table apparently has an integer data type. You can check the data type by querying EXPLAIN Testing. If it has an integer data type, the number value is coerced to an integer before it stored in the table. If you wish to store...

In VBA it is quite simple, since the VBA Round function does that kind of Rounding: Function VBARound(N As Double, Optional NumPlaces As Long = 0) As Double VBARound = Round(N, NumPlaces) End Function The NUMPLACES will allow you to optionally round to other than a whole number....

Having looked at the issues with negative numbers and using it with the find function; a possible solution would be to format your search range and number to find with the same number format. (Based on code supplied by @Gary's Student) Code is below for this. If you don't want...

How about ?signif ? (Depending on your application you could also use print(...,digits=3)) vec <- c(1.739362e-08,8.782537e-08,0.5339712) signif(vec,digits=3) ## [1] 1.74e-08 8.78e-08 5.34e-01 print(vec,digits=3) ## [1] 1.74e-08 8.78e-08 5.34e-01 It's fairly hard to get R to format the elements of a vector differently from each other: usually it assumes you want...

If you have a look at modena.css file, for the selected tab pseudoclass, there is this rule for the border radius: .tab-pane:focused > .tab-header-area > .headers-region > .tab:selected .focus-indicator { -fx-border-radius: 2, 1; /* looks sharper if outer border has a tighter radius (2 instead of 3) */ } You...

css,internet-explorer,width,rounding,calc

It would seem that the suggestion by @web-tiki with: width:33.33%; is the best option....

javascript,function,rounding,deciphering

eX is a valid part of a Number literal and means *10^X, just like in scientific notation: > 1e1 // 1 * Math.pow(10, 1) 10 > 1e2 // 1 * Math.pow(10, 2) 100 And because of that, converting a string containing such a character sequence results in a valid number:...

excel,excel-formula,rounding,worksheet-function,time-format

Adding a ColumnE for Lunch, please try: =IFERROR(IF(D2="","",(D2-A2)*24)-E2,"") formatted as number....

Cast it as a decimal instead of rounding: CAST((((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)*.75)+ (Sum(case when S.StatType = 'Pit' then S.CashIn + S.CreditIn + S.ChipsIn + S.FrontIn - S.CashOut Else 0 end)*.4))/Nullif(Count(Distinct(S.GamingDate)),0)) AS DECIMAL(18,2)) as ADL ...

use bar1 = (store1+500)/1000; System.out.print("Store 1: "); for(int i = 1; i <= bar1; i++) System.out.print("*"); ...

javascript,jquery,regex,rounding

RegEx Let's start with your RegEx. What you have will match decimal numbers with any number of decimal places, but also other things. ^((?:\d+(?:\.\d*)?)|(?:\.\d+))$ Explanation First of all we declare that if there is only an integer without decimal places, that's fine. However, we also allow the short hand .8...

It is rounding correctly but you fail to understand that the value is not the format. There is no difference between the two values, 40000 and 40000.00, and you'll have a similar issue with something like 3.1. Simply use formatting to output whatever number you have to two decimal places,...

python,python-3.x,time,rounding,decimal-point

Use the second argument of round which indicates the number of numbers after the decimal: send_time_ms = time.time() ... recv_time_ms = time.time() rtt_in_ms = round(recv_time_ms - send_time_ms, 3) ...

COBOL does no rounding, unless you tell it to. What it does do, if you don't tell it to do rounding, is low-order truncation. Some people may prefer to term that something else, it doesn't really matter, the effect is the same. Truncation. Negative values are dealt with in the...

java,rounding,bigdecimal,divide

It all has to do with the scales of the BigDecimals involved. When you specify exponential format in the constructor that takes a String, the scale may be negative. This indicates that the significant digits don't extend all the way down to unity. BigDecimal oneEPlus8 = new BigDecimal("1.0E+8"); System.out.println(oneEPlus8.scale()); This...

floating-point,rounding,msp430

Conversion by bit operations is straightforward, and is demonstrated by the C code below. Based on the comment about the data types on the MSP430, the code assumes that int comprises 16 bits, and long 32 bits. We need a means of transferring the bit pattern of the float to...

If you want to chose the power of two, the simplest way is to multiply by e.g. 16, round to nearest integer, then divide by 16. Note that division by a power of two is exact if the result is a normal number. It can cause rounding error for subnormal...

sql,sql-server,select,rounding

You are correct, round is the wrong tool for this job. Instead, you should use floor and ceiling. Unfortunately, they do not have a precision parameter like round, so you'd have to simulate it using division and multiplication: SELECT FLOOR(value * 100) / 100 AS rounded_down, CEILING(value * 100) /...

try this: function getSmooth(meteo, usage) { meteo = Math.round(meteo*100); usage = Math.round(usage*100); return { evo: (meteo + usage) / 100, meteo: meteo / 100, usage: usage / 100 }; } to test for errors in calculations in evo separate from your visualisation logic: var evo, meteo, usage; // ... //...

Use the built-in function round(), example: >>> round(4.7532,2) 4.75 >>> round(4.7294,2) 4.73 ...

c#,rounding,.net-micro-framework

You can try Multiplying your value by 100, round and then divide the result by 100.

Not the rounding makes it -0. The $round variable contains this before the last line: string(3) "0.0" You can verify this with adding this line: var_dump($round); before the echo. So if you multiply "0.0" (string) with -1 then the result will be "-0" Because (string)0 is casted to (float)0 before...

If you want to convert given value 2167.12" to "2167.13" Please use this select ceil(2167.124*100)/100 from dual;...

You can java interop (Math/(floor|ceil). E.g.: user=> (int (Math/floor (/ 3 2))) 1 user=> (int (Math/ceil (/ 3 2))) 2 ...

First of all, make c a double, then use c = (a + b)/2.0 otherwise you have truncation due to division of ints being casted to int. In this way, (a + b) / 2.0 is a double, due to the denominator being a double, so you don't have any...

If you know that you are going to be diving by 100, you can just round first then divide: var round = Math.round(value)/100; //still equals 172.45 However, if you don't know what you are going to be diving with, you can have this more generic form: var round = Math.round(value/divisor*100)/100;...

It appears that the implementation of Float::round, at least for f32 and f64, forward to the roundf32/roundf64 instrinsics, which themselves are implemented using the LLVM functions llvm.round.f32 and llvm.round.f64. The documentation for llvm.round.* doesn't say anything about how to control the rounding mode, sadly. There doesn't appear to be anything...

It's not the precision of the decimal numbers, it's how the underlying floats are stored in Java. This demonstrates: <cfoutput> <cfloop array="#[0.275,0.285,0.295]#" index="s"> #s.getClass().getName()# <cfset f1 = s + 0> #f1.getClass().getName()# #f1.toString()# <cfset f2 = f1*100> #f2.toString()# #round(f2)#<br> </cfloop> </cfoutput> Output: java.lang.String java.lang.Double 0.275 27.500000000000004 28 java.lang.String java.lang.Double 0.285 28.499999999999996...

How about this? z = (Math.abs(z-x1)<Math.abs(z-x2))?x1:x2; If you're sure x1 < x2, then: z = ( (z-x1) < (x2-z) ) ? x1 : x2; ...

c,algorithm,math,optimization,rounding

If your operand is non-negative, how about: unsigned int roundupdiv4 (unsigned int n) { return (n+3)>>2; } Note that any sensible compiler will compile /4 of an unsigned int as >>2 anyway. I can confirm that by compiling the above with gcc -O3 -S: .file "x.c" .text .p2align 4,,15 .globl...

I'd divide the value by 45°, round, and then multiply back by 45°. Pseudocode: deg = round(deg / 45) * 45; Assumption: the / operator returns a floating-point representation and does not perform integer division. If the specific language does not treat it like this it should be handled explicitly...