rounding

Round to nearest five C#

Hi! I'm using C# and I need to round a double to nearest five. I can't find a way to do it with the Math.Round function. How can I do this? What I want: 70 = 70 73.5 = 75 72 = 70 75.9 = 75 69 = 70 and so on.. Is there an easy way to do this? ...

Rounding to the Nearest Ending Digits

I have the following function that rounds a number to the nearest number ending with the digits of $nearest, and I was wondering if there is a more elegant way of doing the same. /** * Rounds the number to the nearest digit(s). * * @param int $number * @param int $nearest * @return int */ function roundNearest($number, $nearest, ...

MS Access Rounding Precision With Group By

Why doesn't the average of the score of an employee of each month, when summed, equal the average of the employees score (ever)? Average SELECT Avg(r.score) AS rawScore FROM (ET INNER JOIN Employee AS e ON ET.employeeId = e.id) INNER JOIN (Employee AS a INNER JOIN Review AS r ON a.id = r.employeeId) ON ET.id = r.ETId WHERE (((e.id)=@em...

SQL rounding and truncation, needs a thorough explanation

Hello. I'm a novice when it comes to SQL and PHP, and I'm trying to round an average price result and take off the extra zeroes that currently appear. Currently my result turns up as: $3.005000 My code currently reads as follows: $result = mysql_query("SELECT AVG(price) FROM milk WHERE price > 0 "); $row = mysql_fetch_row ($result); ...

php float calculation 2 decimal point

Hi everyone, Got another math calculation problem again. $a = 34.56 $b = 34.55 $a do some calculation to get this figure $b is doing rounding nearest 0.05 to get this figure what happen is $c = $b - $a supposedly I should be -0.01, but I echo out the $c is show -0.00988888888888 I try to use number_format($c, 2), but the output i...

Rounding Error in C#: Different results on different PCs

I have a function in C# that returns the following: ... float amount = smallestPercentage * (float)quantity; return (int)amount; Now I know I am suppose to use Convert.Int32(amount) rather than type cast an int, and that has fixed the problem. But my problem was really this... When developing my program at home (Windows Vista) I wou...

How to round with no trailing zeros in SQL Server 2005?

How to round with no trailing zeros in SQL Server 2005? select round(100.5555, 2) ...yields 100.55**00**. How to get rid of the zeros? ...

JavaScript: Rounding to two decimal places. Not less than two

Hello all, I have this line of code which rounds my numbers to 2 decimal places. But the thing is I get numbers like this. 10.8, 2.4 etc. These are not my idea of 2 decimal places so how I can improve this: Math.round(price*Math.pow(10,2))/Math.pow(10,2); I want numbers like 10.80, 2.40 etc. Use of JQuery is fine with me. Thanks for...

How to properly display a price up to two decimals (cents) including trailing zeros in Java?

There is a good question on rounding decimals in Java here. But I was wondering how can I include the trailing zeros to display prices in my program like: $1.50, $1.00 The simple solution of String.format("%.2g%n", 0.912385); works just fine, but omits the trailing zero if it is at the last decimal place. The issue comes up in my pro...

Precision nightmare in Java and MSSQL

Hi everyone, I've been struggling with precision nightmare in Java and MSSQL up to the point when I don't know anymore. Personally, I understand the issue and the underlying reason for it, but explaining that to the client half way across the globe is something unfeasible (at least for me). The situation is this. I have two columns in ...

Faster implementation of Math.round?

Are there any drawbacks to this code, which appears to be a faster (and correct) version of java.lang.Math.round? public static long round(double d) { if (d > 0) { return (long) (d + 0.5d); } else { return (long) (d - 0.5d); } } It takes advantage of the fact that, in Java, truncating to long rounds in to ...

Adding text to this line

I'm a novice so please excuse the simplicity of this. I am trying to get a total ammount, trim the excess decimal places than append some text.. it all works until I try ROUND it ROUND(CAST(ServiceFee * COUNT(UserID) AS VARCHAR(20)),0) + CAST(' yen' as VARCHAR(20)) Thanks in advance ...

Odd rounding problem using the ruby printf-format-specifier

Has anybody got any ideas on this one? When we run: printf("%.0f", 40.5) On a windows box the return is "41" but on our production ubuntu server we're getting "40" ...

accurate single pass penny computation

We have a cost C which must be assigned to departments 1..n. Another computation produces the share for each department, it is a number from 0 to 1, with more than 5 decimal places. The sum of all department shares is exactly 1, but they are not necessarily equal. The goal is to compute the exact dollars and cents to bill to each depa...

creating a custom attribute to control rounding behavior

I have a class with a lot of Decimal properties that are used for financial calculations. There are rules that specify how many decimal places to use when rounding each number. There is no global rule - some are two decimal places, some 0, some 8, etc. I'm trying to figure out the easiest way to approach this. I want to avoid having ...

Rounding to use for int -> float -> int round trip conversion

I'm writing a set of numeric type conversion functions for a database engine, and I'm concerned about the behavior of converting large integral floating-point values to integer types with greater precision. Take for example converting a 32-bit int to a 32-bit single-precision float. The 23-bit significand of the float yields about 7 dec...

Round to n Significant Figures in SQL

I would like to be able to round a number to n significant figures in SQL. So: 123.456 rounded to 2sf would give 120 0.00123 rounded to 2sf would give 0.0012 I am aware of the ROUND() function, which rounds to n decimal places rather than significant figures. ...

How to round a DateTime in MySQL?

I want to discretize the DateTime with the resolution of 5 minutes. I did it in C#, but how to convert the following code to MySQL? DateTime Floor(DateTime dateTime, TimeSpan resolution) { return new DateTime ( timeSpan.Ticks * (long) Math.Floor ( ((double)dateTime.Ti...

ASP Round function not behaving as expected

I'm rounding up my decimal values to integer values ex: I have 5 numbers whose % sum should end up being 100% but i get 101% my values are 11.11111, 80.55555, 5.55555, 2.77777 and on rounding them i get 11, 81, 6, 3 => 101 In above for example 5.555556 rounds up to 6 (correct value) but is screwing up my total for percentage wh...

Consistent rounding of floating points in Ruby

I understand due to the inexact representation of floating points, the following code 'feels' inconsistent. "%.1f" % 1.14 # => 1.1 "%.1f" % 1.15 # => 1.1 "%.1f" % 1.16 # => 1.2 "%.0f" % 1.4 # => 1 "%.0f" % 1.5 # => 2 "%.0f" % 1.6 # => 2 However, is there an easy way of doing consistent floating points rounding by 5? One way might be t...