float input = whatever; long output = (long)(0.5f + input); This is inefficient for my application on an MSP430, using the compiler-supplied floating point addition support library. I'm musing that there may likely be a clever 'trick' to this particular kind of 'nearest integer' rounding, avoiding plain floating point addition...

I have noticed that the formline method in Perl 5 have some strange action. If the decimal value greater than 0.5, it will round up. If the decimal value less than 0.5, it will round down. But when the decimal value is exact 0.5, we normally should round up but...

I'm taking a Python course at Udacity, and I'm trying to work this out for myself without looking at the answer. Perhaps you can give me a hint for my logic? Below are the instructions and what I have so far. We haven't learned conditional statements yet, so I can't...

https://github.com/TelerikAcademy/CSharp-Part-1/blob/master/4.%20Console%20In%20and%20Out/README.md Problem 3, here's my code: Console.Write("Please enter circle radius: "); double Radius = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("\nPerimeter is: {0:F2}", Math.PI*2*Radius); Console.WriteLine("Area is: {0:F2}", Math.PI*Radius*Radius); Problem 5, here's my code: Console.Write("Enter first number: "); int a = Convert.ToInt32(Console.ReadLine()); if (a<0 || a>500) { Console.WriteLine("number A out of bounds!");...

I have an integer number n and I need to rounds n/4 upward. For performance reason I need to find out a fast way in C. The division by 4 can be done using a >> 2 shift operation but I don't know about the round. I could use ceil...

I write numeric data to MS Access tables. All the tables have the same numeric fields format (ie 'real simple', 'standard', 'auto'). I push numbers via Matlab in the tables and MS Access rounds numbers to the 1rst decimal and i can't modify it. Does someone know ? Screenshots there...

Is there an optimized, performant way to round a double to the exact value nearest multiple of a given power of two fraction? In other words, round .44 to the nearest 1/16 (in other words, to a value that can be expressed as n/16 where n is an integer) would...

The following code: double doubleValue = 1713.6; float floatValue = 1713.6f; String fs = "%-9s : %-7s %-7s\n"; System.out.printf( fs, "", "double", "float" ); DecimalFormat format = new DecimalFormat("#0"); System.out.printf( fs, "toString", String.valueOf( doubleValue ), String.valueOf( floatValue ) ); format.setRoundingMode( RoundingMode.DOWN ); System.out.printf( fs, "DOWN", format.format( doubleValue ), format.format( floatValue...

I'm looking for a regex and a JavaScript function that rounds up what the user just input to 2 decimals max, the user can input any number and dots e.g. 1, 2.3490, 23 and it should change when the user clicks away from the field to 1.00, 2.35, 23.00. I've...

I have the same question as the question here. The user asked: I just wrote this to return the nearest 45 degree angle. Like 0, 45, 90, 135... It feels like there must be a better way of doing this though. This is working fine but it looks messy. public...

Why is Python rounding like this? >>> 1.12345678901234567890 1.1234567890123457 >>> 0.0001100110011001100110011001100110011001100110011 0.00011001100110011001 ...

I am trying to create a function (my.func) that could round a number/numeric vector/matrix/data.frame conditionaly to its value. For example: my.func(1630.123) will give 1630 and my.func(0.123) will return 0.12 I have tried: my.func <- function(x0, ...) { unlist(lapply(x0, function(x) { if(x >= 100) return(round(x, 0)) if(x >= 10 & x...

I'm trying to calculate accurate capscrew lengths between to connections where one side is drill & tap and the other is a flange. For some reason I can't post the image; essentially I've got a bunch of dimensions from B4 through B10 with a yes or no on B8 followed...

I've found tons and tons of information about rounding a float to a specific number of decimal places by using 'scale' with BC and '%.xf' with printf, but if I'm working with numbers that don't always have the same format like I've shown below, is there a way to round...

In Postgres SQL: Is it possible to get 1 digit number from a double number? For example: I have 27/10 = 2.7 I WANT TO GET the number "2" only (as number not string) I tried to convert 2.7 into integer but it will be rounded into 3. How can...

I currently have the following formula to track my work schedule. Time is put down in traditional format ie. 8:00 am, and is then totaled to show hours worked as a whole number 8, for 8 hrs worked: =IF(D2="","",ROUND(IF(OR(C2="",B2=""),(D2-A2),(B2-A2)+(D2-C2))*24*4,0)/4) A=Time In , B=Lunch Out , C=Lunch In , D=Time Out....

I'm reading CS:APP, and regarding casts it says that when casting from int to float, the number cannot overflow, but it may be rounded. It seemed odd to me as I didn't know what there was to round, so I've tried it out. I thought that this would only be...

I have a simple UDP client–server program and I get the time in ms of sending a packet and the time in ms of receiving the packet like this: send_time_ms = int(round(time.time() * 1000)) # sending the packet # ... # receiving the packet recv_time_ms = int(round(time.time() * 1000)) For...

In COBOL what is the result of COMPUTE RESULT1 = 97 / 100 COMPUTE RESULT2 = 50 / 100 COMPUTE RESULT3 = 32 / 100 COMPUTE RESULT4 = -97 / 100 COMPUTE RESULT5 = -50 / 100 COMPUTE RESULT6 = -32 / 100 When RESULT1/2/3 are: PIC S9(4) PIC S9(4)V9...

#include <iostream> #include <iomanip> int main() { float f = 0.115; std::cout << f << std::endl; std::cout << std::fixed << std::setprecision(2) << f << std::endl; return 0; } The output of the above code is: 0.115 0.12 It makes sense that 0.115 is rounded to 0.12. However, when we change...

<?= round(number_format($vehicle->prices['total_rental'], 2, '.', ',') * 0.50,2) ?> I would like the output to round up to the next 2 decimal places (for money / dollars & cents). This works great when $vehicle->prices is below $1,000. When $vehicle->prices is above $1,000, it just outputs .50. What is the correct way...

I'm using the ng-currency library - http://aguirrel.github.io/ng-currency/ - to display currency values in a project. However, it's not exactly what I need, the currency value cannot be negative cannot be greater than 999,999,999 must be rounded to the nearest whole number i.e. contain no decimal rendered as a currency value,...

I am writing a test program to test the RoundingMethod for BigDecimal. I am relatively new to Java (I've only been at it for five days!!) . I have written a class in DrJava to test this but having assigned pp as the variable for BigDecimal , how do i...

I'm wondering how to round a number in python while using string formatting. In my code, I used %r instead of %d, beacuse %d prints an interger. How do I round the numbers while using %r? I would like to have my numbers rounded to two decimal places. def new_formula(value):...

Is this the right way to convert a microseconds and represent it as a timeval in my application ? I require the usage of a struct timeval and the above code is part of a routine that converts a double microsecond to a timeval NEGATIVE Value in this code //UPDATE...

I have to do a special type of rounding in excel for my school project. If the number is bigger than #.5 I have to round it up. If the number is equal to #.5 I have to round it to the nearest even number. If the number is less...

Does anyone know why the pmulhrsw instruction or _mm_mulhrs_epi16(x) := RoundDown((x * y + 16384) / 32768) always rounds towards positive infinity? To me, this is terribly biased for negative numbers, because then a sequence like -0.6, 0.6, -0.6, 0.6, ... won't add up to 0 on average. Is this...

I have this code (@Model.TW is 11,58 and @Model.TK is 27,65)(both doubles): var parameters = [@Model.TW,@Model.TK]; When checking parameters[0] and [1] the double values are rounded to 11 and 27. Why is this? The same happens when just doing var double = [@Model.TW]; or var double = parseFloat('[@Model.TW]'); even if...

I've written many PHP applications and have just stumbled upon a weird scenario. The round function doesn't seem to be working in a very complex set of code. I've tried many different things before calling round, but this just doesn't work. The function returns 0.56000000000000001 when given round($val,2), with $val=0.5600....

I am using NewtonSoft's JSON.NET assembly to load a .json file in a C# console application. I think I have done most of the work except I am finding that some negative float values are being rounded. Here is relevant code; as you can see, I have tried both load...

I´m triying to round on c# different numbers according to some conditions: if number is between 0 and 100, round it to nearest integer. Ex: 8.4 = 8, 8.6 = 9 if number is between 101 and 10000, round it to nearest hundred. Ex: 1545 = 1500, 1551 = 1600...

I got curious about a rounding algorithm, because in CS we had to emulate an HP35 without using the Math library. We didn't include a rounding algorithm in our final build, but I wanted to do it anyway. public class Round { public static void main(String[] args) { /* *...

So, i got two numbers, say, A and B. A is any positive number and B is percentage between 0 and 100. What i need is percentage B of A, but randomly round up or down, e.g. A = 15, B = 50, result=7(50%) or 8(50%) A = 10, B...

I am trying to round decimal number upto two decimal places which is working perfectly. I am doing as below : Math.Round(Amount, 2) So, if I have Amount as 40000.4567, I am getting 40000.46which is exactly what I want. Now problem is I have decimal number like 40000.0000, when I...

Is this a bug or some math thing I don't understand? round(58.900662, 2) => 58.9 Where is the last zero? EDIT: In my opinion, the documentation is lacking information. The following code does as expected number_format(round(58.900662, 2), 2) => 58.90 ...

I building a website using the (excellent) Flask framework in which I now want to display some numbers. The round filter provided by jinja2 works fine, except for when there is no decimal value: {{ 1.55555|round(2) }} -> 1.56 {{ 1.5|round(2) }} -> 1.5 {{ 1.0|round(2) }} -> 1.0 {{...

I want to round off the number with exact value of decimal in PHP. Eg: 1.399 => 1.39 or 363.3 => 363.30 I have used printf("%.2f", 1.399) and number_format((float)$a,2,'.','') but the answer is 1.40 which is incorrect for me....

I want to round down current time in 10 minutes block. I can do so either on Java, php or mysql (preferably), whatever is considered better. For example: 08:49:50 -> 08:40:00 14:23:12 -> 14:20:00 09:32:11 -> 09:30:00 12:20:00 -> 12:20:00 I tried to solution from Round date to 10 minutes...

I am using jdk 1.8.0_45, and our tests discovered a bug in rouding. RoundingMode.HALF_DOWN works the same as RoundingMode.HALF_UP when the last decimal that decide the rounding is 5. I found related issues with RoundingMode.HALF_UP, but they are fixed in update 40. I also put a bug to the oracle,...

I transformed my data into a boxplot (used geom_boxplot of ggplot), so that the outliers got visible. Afterwards I wanted to remove them from my data. That is why I used "ggplot_build" to get on all the informations of the plot and saved it with a new name. Outlier_boxplot<-ggplot_build(boxplot) Now...

I am using Python 2.7 and MySQLdb. I am trying to update and set as the number the decimal I have set as data but what I get is an the closest integer. This is the code: Value = 5 data = 5 data = data + 0.5 print(data) x.execute("""...

I wanted to get the same result in javascript from this line in c#: round = Math.Round((17245.22 / 100), 2, MidpointRounding.AwayFromZero); // Outputs: 172.45 I've tried this but no success: var round = Math.round(value/100).toFixed(2); Thanks in advance :)...

This question already has an answer here: Limiting floats to two decimal points 11 answers So I was surprised I didn't find anything regarding this. I have a python script which is testing a C++ program. It needs to format a float in the same way std::setprecision does. That...

Now it looks like: 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 = 82.7959410430839 It should look like: 0 15.39 15.39 27.59 27.59 46.06...

This is an easy one. But anyway, I think it is a good idea to have this question answered here for a faster, easier reference. This operation: (/ 3 2) yields this: 3/2 I need one function to round up, which would yield 2 and another one to round down,...

In scientific paper writing, values are typically reported as rounded values with one, two or three digits, often depending on the type of value reported. This means that everytime when e.g. including an inline mean value in Rmarkdown I have to add round to the code. Thus I am wondering:...

I'm making a little app to analyze geometry. In one part of my program, I use an algorithm that has to have a convex object as input. Luckily, all my objects are initially convex, but some are just barely so (see image). After I apply some transformations, my algorithm fails...

This question already has an answer here: get closest number out of array 9 answers I have two numbers wich can change, like x1 and x2. Then I also have an other number what is random but always between x1 and x2, like z. What I want to do...

Problem I get numbers from 1 to 5 including all possible floating point numbers in between. The output must contain two digits after comma and in case of after-comma digits they need to be rounded down (floor). Example input and output: 1 -> 1.00 4.3 -> 4.30 1.1000 -> 1.10...

Why does the following query result in the value 3.21865081787109E-06 when the value should be zero? CREATE DATABASE TEST GO USE TEST CREATE TABLE NUMS (NUMBER REAL) GO INSERT INTO NUMS VALUES (1.67460317460317),(-1.6746) GO Then run: SELECT SUM(NUMBER) FROM NUMS This returns the value 3.21865081787109E-06? If I cast the values...

I'm learning JavaScript, and I've heard that there can be issues among different browsers when using JavaScript's Math.round() and .toFixed() methods. After researching on this website, the solution I've with the seen with least criticisms are the Decimal Rounding method as illustrated at the MDN. Looking over the code, I'm...

int a, b, c = 0; c = (a+b)/2; In this code, if both "a" and "b" are even (Example 1), then there's no problem. But if one of them is odd (Example 2), then the answer will have +0.5. I want to round it up. Example 1. a=4, b=10...

I'm having an issue with a VBA macro I wrote, with the part that is intended to find a lowest value in range. The line looks like this: Min = Application.WorksheetFunction.Min(a0eqB) Set MinCell = a0eqB.Find(Min, LookIn:=xlValues) And it returns Object variable or With block variable not set error. Now, I...

I have a 3 column layout that should fill the whole screen so on my columns I am using: width: calc(100% / 3); So lets say for example my screen is 782px wide: 782 / 3 = 260.66̅ However the problem I have is in Internet Explorer, where it rounds...

I want to convert this value "2167.124" to "2167.13" using round function in oracle 11g. Problem is that it converts LIKE "2167.12" But i need "2167.13". Please help, Thanks in advance...

Can some one explain why I get 0 as result for the first line? System.out.println((new BigDecimal("1.0E+8")).divide(new BigDecimal(100000000), BigDecimal.ROUND_HALF_UP)); System.out.println((new BigDecimal("1.0E+8")).subtract(BigDecimal.ONE).divide(new BigDecimal(100000000), BigDecimal.ROUND_HALF_UP)); 0E+7 1 ...

This question already has an answer here: PHP: show a number to 2 decimal places 11 answers I have a variable $amount = 621.00; I need the 30% of the variable with output 186.30 When I calculate: $amount2 = round($amount*30/100 , 2); echo $amount2 gives the output of 186.3...

I'm building a financial website with the Flask framework and I'm currently writing unit tests for it. I'm using the Babel package to format money amounts and I hit upon rather strange rounding behaviour. I would expect rounding to be up in case of a 5, or to at least...

I understand that there can be a .000000000000001 margin of error for double math and this is be made worse by multiplication to make the margin of error larger. With that said, is it possible to round off every calculation to a significant digit (maybe 4 decimal places) to achieve...

I am concerned about the following question. If I apply the function by(productivity$LOC, productivity$extension, stat.desc, norm = TRUE, basic = TRUE) how can I round the output values of the by() function? ...

function Round2DecimalPlaces(l_amt) { var l_dblRounded = +(Math.round(l_amt + "e+2") + "e-2"); return l_dblRounded; } Fiddle: http://jsfiddle.net/1jf3ut3v/ I'm mainly confused on how Math.round works with "e+2" and how addition the "+" sign to the beginning of Math.round makes any difference at all. I understand the basic of the function; the decimal...

bar1 = store1/1000; System.out.print("Store 1: "); for(int i = 1; i <= bar1; i++) System.out.print("*"); How to show number of "*" rounded up or down to the nearest thousand. Right now it just rounds down....

I am trying to perform a Tuckerman Rounding Test in order to determine the correctly rounded to nearest result. I created a program in C++ to compare two solutions to a square root of a number and perform a tuckerman test on them. However, the C++ math library solution fails...

I am trying to create a program in C# using Microsoft Visual Studio. The application should be able to calculate the area and boundary lengths for Circles, Squares and Right-Angled Triangles; it must also draw each of these shapes on the screen at the currently set size. One of the...

I'm having the weirdest problem with my game in java. :| I can get around it, so I will be able to continue on this game without a solution, but I just thought it was really weird. It's a 2D Minecraft-ish (I know, I know, it's just for practice) game,...

I would like to be abble to round up or down 10.823. Expected result: rounding down = 10.82 rounding up = 10.83 Knowing that round(10.823, 2) only rounds down. How to round it up?...

i'm developing a PHP application and i have one problem when i use HTML text inputs, and this is the first time where i deal with this case. When i try to insert a float value as 34.4 in HTML text input to put it in MySQL DB, but when...

This question already has an answer here: How do you round UP a number in Python? 9 answers Python round up integer to next hundred 6 answers so i'm making a pizza program in Python 3.3 that takes input from the user and prints the amount of pizza's needed....

I need some help with rounding 0,73 to 73 with math.round I am having troubles finding out how. Teacher wants me to make a method that takes in a decimal and return an integer. Yes with Math.Round (); . On C#

I need a formula on Excel which would deduct the multiple of 20 from a given number. For example: 2 would give you 18, 10 would give 10, 23 would give 17, 118 would give 2, 321 would give 19....

This is a question about precision, rounding, data types, and using the xs:integer() function in XSLT 2.0. We have the following line of code: <xsl:value-of select="xs:integer(round(InvoiceAmount * 100))"/> When the value of InvoiceAmount in the source XML is -62.84, the result of the value-of is -6283 which is incorrect. We...

Suppose that I have: double Nc= 7.6695805E-4; NumberFormat formatter = new DecimalFormat("0.00000000000"); String string = formatter.format(Nc); System.out.println(string); the output would be: 0.00076695805 But it is place in string type variable! What i have to do is to convert it into double or float or long so that it should remain...

I would like to do this: if I have 1234.5678, I would like 1 234.57. I have tried several things, like: Object theValue = theValues.get(theHeaderName); DecimalFormatSymbols theSymbols = new DecimalFormatSymbols(); theSymbols.setGroupingSeparator(' '); DecimalFormat theFormatter = new DecimalFormat("#.00", theSymbols); el.text(theFormatter.format(theValue)); But I don't manage to have the rounding and the separator....

Suppose I want to round numbers that have mantissa greater than 0.3 'up' and those below 'down'. How can I do it in Java? The only thing that came to my mind was Math.round(), but I can't seem to make it follow a certain rule....

I have an object containing decimals values : evo, meteo and usage I try to display this values with three conditions : values can be positive and negative values must be displayed as percentage without any decimals (A) must be true (A): Math.round(meteo*100)+Math.round(usage*100) = Math.round(evo*100) For example, if we apply...

I'm creating an Android app and I need to calculate the movement speed of the device. I do this by taking an average of some earlier locations' speeds. However, I need the speed to be in km/h as an int, so I use (int)Math.ceil(speedAsDouble) However, this always equal to 1,...

Suppose I have a numeric vector which I'd like to round to 'prettier' numbers, such as: vec <- c(1.739362e-08,8.782537e-08,0.5339712) I'd like it to be: pretty.vec <- c(1.74e-08,8.78e-08,0.53) How do I achieve that? using round doesn't really help since it rounds the first two elements to 0: > round(vec,2) [1] 0.00...