简体   繁体   中英

Formatting decimal with maximum total number of digits

Requirement is to format a decimal number to a string, but with a maximum of 10 digits in total, for example:

  • 7846.05368740952 -> "7846.053687"
  • 47585.7350421593 -> "47585.73504"

Using {0:0.######} obviously doesn't work because it doesn't take into account the total number of digits... is there a formatting string that does this kind of formatting, or does it require extra code to achieve this?

EDIT: I'm trying to set a cell format with Aspose.Cells using the Custom property on the style of a cell. It seems that G10 doesn't work.

Probably, you're looking for "G10" format string

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

this formatting works with Decimal as well:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM