Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Specify precision of decimal? 2

Status
Not open for further replies.

jasonp45

Programmer
Aug 23, 2001
212
US
I have a decimal variable. I'd like it to store a number rounded to two decimal places.

I've declared and loaded it like this:

Code:
private Decimal FileSize_MB_DataFile = 0.0M;
this.FileSize_MB_InitialFile = Convert.ToDecimal(fileInfo.Length) / 1048576; //1024 x 1024

I want the actual value of the variable to be, for example, .75 or 15.28 or whatever (I don't want to convert it to a string and format it unless that's strictly necessary). When I look at the value of the variable in the debugger it goes out to like 15 decimal places. I'm sure this is quite simple. Thanks.
 
Wouldn't it make more sense to just leave it with as much precision as possible? The computer could care less how much precision you force it to have. Right now your complaint is that it looks ugly in the debugger, so just ignore the debugger and format it when you display the value to the user. Throwing away data isn't usually the answer.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I would agree to leave the precision, toy could always format the variable in the debugger watch window, so that it is easy to look at, but overall, I wouldn't be concerned
 
I do not need or want the full precision. I am not concerned with how it looks in the debugger; I know exactly how much precision I need for this value (two decimal places) and that is what I want.

Is there no simple way to specify the precision of a decimal?
 
You want to do all your conversions at the highest precision possible but then save your final result to 2 decimal places.

Just display your output using

Math.Round(234.567890, 2);

 
Ehhh....your foot. Shoot away:

FileSize_MB_DataFile = decimal.Round(FileSize_MB_DataFile, 2);

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Or use an extension method, something like:

Code:
	public static class ExtensionMethods
	{
		public static decimal Rounded(this decimal value, int decimalplaces)
		{
			return Math.Round(value, decimalplaces);
		}
	}

which can be called using:

Code:
		private decimal MyDecimal;
		private void button1_Click(object sender, EventArgs e)
		{
			MyDecimal = 123.456M;
			decimal x = MyDecimal.Rounded(2);
			MessageBox.Show(x.ToString());
		}


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top