Formatting the decimal number with the maximum number of digits

The requirement is to format the decimal number in a string, but not more than 10 digits in total, for example:

  • 7846.05368740952 → "7846.053687"
  • 47585.7350421593 → "47585.73504"

Using {0: 0. ######} obviously does not work, because it does not take into account the total number of digits ... is there a format string that does this kind of formatting, or does it require additional code?

EDIT: I am trying to set the cell format using Aspose.Cells using the Custom property in the cell style. G10 seems to not work.

+4
source share
1 answer

Perhaps you are looking for a format string "G10"

   Double s = 7846.05368740952; 
   // 7846.053687
   String result = s.ToString("G10");

Decimal:

   Decimal d = 47585.7350421593M;
   // 47585.73504
   String result = d.ToString("G10");
+1

Source: https://habr.com/ru/post/1616184/


All Articles