Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
String manipulation
Message
De
23/12/2010 14:58:18
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01493708
Message ID:
01493740
Vues:
62
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?
>
>TIA
>
>Bob


I've always adjusted it using math prior to going ToString():

tempValue = (int)Math.Round(value * (decimal)Math.Pow(10, dPos), 0);

John
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform