Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
String manipulation
Message
From
23/12/2010 12:20:37
 
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:
01493718
Views:
69
This message has been marked as a message which has helped to the initial question of the thread.
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



>Hello and happy holidays to all,
>
>I am trying to manipulate a decimal value for outputting to a text data file (fixed format) for data interchange. For example, I have a decimal amount of 18.50 and it needs to be output to a fixed format of length 15 with an implied decimal position 3 characters to the left of the end of the string. Therefore the output should be as follows " 18500". That's 10 spaces followed by the number with the implied decimal between the 5 and the 0.
>
>So I created the following function to handle all different variations of formatting numeric strings based on parameters:
>
>
>private static string FormatNumericString(decimal myValue, string justify, bool zeroFill, byte? decimalPos, int strLength, bool? showDecimal)
>        {
>            string myString = " ";
>            decimal tempValue = 0;
>            char padChar = ' ';
>            int dPos = 0;
>            if (zeroFill == true)
>                padChar = '0';
>            if (decimalPos == null)
>                dPos = 0;
>            else
>                dPos = Convert.ToInt16(decimalPos);
>            if (showDecimal == null)
>                showDecimal = false;
>            tempValue = Math.Round(myValue, dPos);
>            myString = tempValue.ToString();
>            if (showDecimal == false)
>                myString = myString.Replace(".", "");
>            if (justify.Contains("R"))
>                myString = myString.PadLeft(strLength, padChar);
>            else
>                myString = myString.PadRight(strLength, padChar);
>
>            return myString;
>        }
>
>
>However, what I am finding out is that the value passed down to myValue is being limited to the number of decimals as defined in the database column it is coming from. In the example I mention above the database column value passed down is defined as numeric(10,2) so when the value is returned, it comes back as " 1850".
>
>Any ideas as to why this is happening and instead of changing the database definition, what can I do to fix this in the C# code?
>
>TIA
>
>Bob
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform