Dynamic number format in .NET?

I have a problem and cannot find a solution. I have numbers (decimal), e.g. 85.12343 or 100 or 1.123324. I want to format this so that the result is always 13 characters, including the delimiter.

100 → 100.000000000
1.123324 → 1.12332400000

I tried with toString but could not. How can i do this?

Thanks:)

+4
source share
7 answers
int digits = 13; decimal d = 100433.2414242241214M; int positive = Decimal.Truncate(d).ToString().Length; int decimals = digits - positive - 1; //-1 for the dot if (decimals < 0) decimals = 0; string dec = d.ToString("f" + decimals); 

He will not delete numbers from the whole part, but only a fraction when necessary.

+8
source

I would go with Kobi's answer if it wasn’t possible that you could start with 13 digits, in which case you might need to do something like this ( warning ): I didn’t even try to make it effective, of course, there are ways to do it optimization if necessary):

 public static string ToTrimmedString(this decimal value, int numDigits) { // First figure out how many decimal places are to the left // of the decimal point. int digitsToLeft = 0; // This should be safe since you said all inputs will be <= 100M anyway. int temp = decimal.ToInt32(Math.Truncate(value)); while (temp > 0) { ++digitsToLeft; temp /= 10; } // Then simply display however many decimal places remain "available," // taking the value to the left of the decimal point and the decimal point // itself into account. (If negative numbers are a possibility, you'd want // to subtract another digit for negative values to allow for the '-' sign.) return value.ToString("#." + new string('0', numDigits - digitsToLeft - 1)); } 

I / O Example:

  Input output
 ---------------------------------------
 100 100.000000000
 1.232487 1.23248700000
 1.3290435309439872321 1.32904353094
 100.320148109932888473 100.320148110
 0.000383849080819849081 .000383849081
 0.0 .000000000000
+4
source

Besides just filling in the string, you can do even more complex math to determine the number of digits:

 String FormatField(Int32 fieldWidth, Decimal value) { var integerPartDigits = value != Decimal.Zero ? (int) Math.Log10((Double) value) + 1 : 1; var fractionalPartDigits = Math.Max(0, fieldWidth - integerPartDigits - 1); return value.ToString("F" + fractionalPartDigits); } 

Please note that if the value is negative or has an integer part with one smaller digit than the field width, you will not get the desired result. However, you can change the code to accommodate these cases based on how you want to format and align these numbers.

+1
source

Fast 'n' dirty:

 return (value.ToString("0.#") + "0000000000000").Substring(0, 13); 
+1
source
 string formatted = original.ToString("0.000000000000").Remove(13); 
+1
source

What about

 string newString; if (original.ToString().Length >= 13) { newString = original.ToString().Substring(13); } else { newString = original.ToString().PadRight(13, '0'); } 
0
source
 int noofdecimal=3; double value=1567.9800 value.ToString("#." + new string('0', noofdecimal)); //Result=1567.980 
0
source

All Articles