Dato0011

Hello is this possible to change decimal format in current thread for example, I want decimal to always look like 2.34 or just 2. How can I do that thanks in advance. :)


Re: .NET Base Class Library Changing Decimal format for CurrentThread

TilakGopi

Hi, u can use Math.Round() function as follows


decimal d = 54.24M;
d = (
Math.Round(d, 0));
//now d will have 54 only
 
d= 54.1234

d = (Math.Round(d, 2));
//Now d will have 54.12
 
d= 54.9678
d = (Math.Round(d, 2));
//Now d will have 54.96
 
d= 54.9678
d = (Math.Round(d, 0));
//Now d will have 55

 

for some other type of conversions see the following posts

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=927566&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=945442&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=870610&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=616609&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1193945&SiteID=1

Thanx,

Ch.T.Gopi Kumar.






Re: .NET Base Class Library Changing Decimal format for CurrentThread

Dato0011

thanks, I know that but what i need is to change the format for the whole thread. without needing to write Math.Round or Convert.ToInt32 etc. I know it's possible but cant find out how.




Re: .NET Base Class Library Changing Decimal format for CurrentThread

TilakGopi

Hi,

Overriding the appropriate method(like ToString() etc) of decimal will help in this case

Thanx,

Ch.T.Gopi Kumar.






Re: .NET Base Class Library Changing Decimal format for CurrentThread

Matthew Watson

You can do this for string conversions, but only if you use the "n" specifier like this: value.ToString("n");

If you just using value.ToString(), it won't work.

Anyhow, you can do it something like this:



double value = 7.654321;

MessageBox.Show(value.ToString("n"), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);

CultureInfo cultureInfo = new CultureInfo("EN-us");
cultureInfo.NumberFormat.NumberDecimalDigits = 1;
Thread.CurrentThread.CurrentCulture = cultureInfo;

MessageBox.Show(value.ToString("n"), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);