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