custom number formatting

C

ChrisB

Hello:

I was wondering if there might be a more efficient way to handle number
formatting over a range of values:

switch(DecimalPlaces)
{
case 1:
formattedResult = result.ToString(".0");
break;
case 2:
formattedResult = result.ToString(".00");
break;
case 3:
formattedResult = result.ToString(".000");
break;
case 4:
etc.
}

I suppose the format string could be dynamically generated, but is there
possibly a more elegent way to accomplish the same task using .NET
non-custom formatting?

Thanks!
Chris
 
C

ChrisB

cody said:
return result.ToString("n"+DecimalPlaces);

Your suggested syntax results in a complilation error:
Argument '1': cannot convert from 'string' to 'System.IFormatProvider'

Chris
 
C

cody

What type does your variable "result" have? My proposal should work with
standard types like decimal,double,int..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top