Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 905
L-Four Avatar asked Oct 31 '22 15:10

L-Four


1 Answers

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");
like image 88
Dmitry Bychenko Avatar answered Nov 15 '22 05:11

Dmitry Bychenko