Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
String manipulation
Message
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01493708
Message ID:
01493771
Views:
58
Hi,
If you use the default .ToString(string format) you don't have to do the rounding yourself - it's built in. So an extension method like this should do what you need:
public static string ToString(this Decimal myValue, int strLength, byte decimalPos, string justify = "R", bool zeroFill = true, bool showDecimal = true)
        {
            char padChar = zeroFill ? '0' : ' ';
            string s = myValue.ToString("." + new string('0', decimalPos));  //This rounds...
            if (!showDecimal) { s = s.Replace(".", ""); }
            string padding = new string(padChar, strLength - s.Length);
            return (justify == "R") ? padding + s : s + padding;
        }
Then, as examples:
string s = 1234.56M.ToString(10, 2, "R");
 s = 1234.56M.ToString(10, 4, "",false);
 s = 1234.56M.ToString(10, 2, "R",true,false);
 s = 134.5645676M.ToString(10, 4, "");
 s = .5645676M.ToString(10, 4, "");
 s = 1.99999M.ToString(10, 4,"",false);
The order of the default parameters might be improved tho....

>I used your advice with a little hint from Viv to split the value at the decimal and right pad if necessary.
>
>Thank you all.
>
>Bob
>
>>Hi Bob, Happy Holidays!
>>
>>I think you probably need to do something before you replace the decimal point. Check that the number of positions past the decimal point equals your decimalPos value, pad with zeroes if it doesn't, then go ahead and remove the decimal point. I think that should solve your problem.
>>
>>~~Bonnie
Previous
Reply
Map
View

Click here to load this message in the networking platform