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:
01493717
Views:
74
This message has been marked as a message which has helped to the initial question of the thread.
>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?

Maybe a different approach:
        static string FormatNumericString(decimal myValue, byte decimalpos, int strLength, bool showDecimal)
        {
            string[] parts = myValue.ToString().Split('.');
           return (parts[0] + (showDecimal ? "." : "") + parts[1].PadRight(decimalpos,'0')).PadLeft(strLength,'0');
         }
I've skipped a couple of parameters but should be a basis.....
Previous
Reply
Map
View

Click here to load this message in the networking platform