rounding

Display a percentage in a label

I am working on a C# windows application. I have a label on my form that I want to display a calculation. Here is my code: this.lblPercent.Text = (Convert.ToString(totalPercent)); I have the variable totalPercent defined as a double, how do I round this number to 2 decimal places? When I run my program, 86.8245614 is being displayed...

How Accurate is DateTime.AddDays?

Since DateTime.AddDays() takes a double parameter, I'm concerned that when you add a days, there might be some rounding errors. For example let's say I have the following loop: DateTime Now = DateTime.Today; for (int i = 0; i < 365; ++i) { Now = Now.AddDays(1); // do something } I'm concerned that Now might start drifting away...

Round Off decimal values in C#

how do i round off decimal values ? Example : decimal Value = " 19500.98" i need to display this value to textbox with rounded off like " 19501 " if decimal value = " 19500.43" then value = " 19500 " ...

Why and how does ROUND_HALF_EVEN minimize cumulative error when applied repeatedly over a sequence of calculations?

Hi, I am told that ROUND_HALF_EVEN is the favored rounding mode for financial data calculations. I am curious to know why and how this rounding mode would reduce the cumulative error as stated in javadoc BigDecimal 1.4.2. Thanks, Dean ...

How to round floats to integers while preserving their sum?

Let's say I have an array of floating point numbers, in sorted (let's say ascending) order, whose sum is known to be an integer N. I want to "round" these numbers to integers while leaving their sum unchanged. In other words, I'm looking for an algorithm that converts the array of floating-point numbers (call it fn) to an array of integ...

how to get numbers to have precision of .05 ?

The following will ensure that any large numbers will only be precise to the hundredths place (related to this answer): public function round( sc:Number ):Number { sc = sc * 100; sc = Math.floor( sc ); sc = sc / 100; return sc; } What is the optimal way to round my numbers to the precision of .05? Is there something ...

.Net Round Bug

The Math.Round function for .Net 3.5 SP1 appears to round 0.5 to zero, while it rounds 1.5 to 2.0. I have tested this with decimal numbers, and the following code: decimal pos = 0.5m; decimal neg = -0.5m; Console.WriteLine("Pos: {0} Rnd: {1}", pos, Math.Round(pos)); Console.WriteLine("Neg: {0} Rnd: {1}", neg, Math.Round(neg)); Console...

Fractional Part of the number question.

What is a good algorithm to determine the necessary fraction needed to add/sub to the number in order to round it to the nearest integer without using the inbuilt ceiling or floor funcitons? Edit: Looking for a mathematical number trick to figure out the part needed to round the number to the nearest integer. The more primitive the mat...

How do I round numbers up to a dynamic precision in Ruby On Rails?

I want to round numbers up to their nearest order of magnitude. (I think I said this right) Here are some examples: Input => Output 8 => 10 34 => 40 99 => 100 120 => 200 360 => 400 990 => 1000 1040 => 2000 1620 => 2000 5070 => 6000 9000 => 10000 Anyone know a quick way to write that in Ruby or Rails? Essentially I need to know the ...

How do I format a double to currency rounded to the nearst dollar?

Right now I have double numba = 5212.6312 String.Format("{0:C}", Convert.ToInt32(numba) ) This will give me $5,213.00 but I don't want the ".00". I know I can just drop the last three characters of the string every time to achieve the effect, but seems like there should be an easier way. ...

How do I round a float up to the nearest int in C#?

In C#, how do I round a float to the nearest int? I see Math.Ceiling and Math.Round, but these returns a decimal. Do I use one of these then cast to an Int? ...

Rounding issues with allocating dollar amounts across multiple people

What is the best way to solve this problem in code? The problem is that I have 2 dollar amounts (known as a pot), that need to be allocated to 3 people. Each person gets a specific amount that comes from both pots and the rates must be approximately the same. I keep coming across rounding issues where my allocations either add up to...

How to show number without rounding off numbers

Hi I have this number: 1234.5678 (as a text) I need this number as double, with only 2 numbers after the dot But without Round the number in 1234.5678 - i get 1234.57 in 12.899999 - i get 12.90 How I can do it ? ...

How can I work around a round-off error that causes an infinite loop in Perl's Statistics::Descriptive?

I'm using the Statistics::Descriptive library in Perl to calculate frequency distributions and coming up against a floating point rounding error problem. I pass in two values, 0.205 and 0.205, (taken from other numbers and sprintf'd to those) to the stats module and ask it to calculate the frequency distribution but it's getting stuck i...

Correct formatting of numbers with errors (C++)

I have three sets of numbers, a measurement (which is in the range 0-1 inclusive) two errors (positive and negative. These numbers should be displayed consistently to the number of significant figures, rounded up, which corresponds to the first non-zero entry in either of the number. This requirement is skipped on the measurement if it...

In C#: Math.Round(2.5) result is 2 (instead of 3)! Are you kidding me?

In C#, the result of Math.Round(2.5) is 2. It is supposed to be 3, isn't it? Is this a C# bug or something? Thanks! ...

SQL Server Automatic Rounding ?

Hi fellows, I am very confused by the following results: PRINT 3.1415926535897931 /180 Console result = 0.01745329251994329500 DECLARE @whatTheHell float(53) SET @whatTheHell = 3.1415926535897931/180 PRINT @whatTheHell Console result = 0.0174533 I don't understand because referring to this: http://msdn.microsoft.com/en-us/libra...

Rounding doubles - .5 - sprintf

I'm using the following code for rounding to 2dp: sprintf(temp,"%.2f",coef[i]); //coef[i] returns a double It successfully rounds 6.666 to 6.67, but it doesn't work properly when rounding 5.555. It returns 5.55, whereas it should (at least in my opinion) return 5.56. How can I get it to round up when the next digit is 5? i.e. return...

Excel RoundUp vs .NET Math.Round

Guys, in Excel, =ROUNDUP(474.872126666666, 2) -> 474.88 in .NET, Math.Round(474.87212666666666666666666666667, 2, MidpointRounding.ToEven) // 474.87 Math.Round(474.87212666666666666666666666667, 2, MidpointRounding.AwayFromZero) // 474.87 My client want Excel rounding result, is there any way I can get 474.88 in .NET? Thanks a lot ...

Large numbers rounding off [c#]

I am having some weird issue here. I have a database table which has huge value stored on a column. My application (C#) is reading this value and keeping in a double type. This application will insert the same value to another table. Note : I am not doing any calculations/processing on the value read from the first table. It is just kept...