2

I have a decimal number :

decimal a = 0.8537056986486486486486486486;

I want to show it as string with only 8 digit after point : a equals -> "0.85370569". How can I do it ?

torrential coding
  • 1,751
  • 2
  • 24
  • 34
Haddar Macdasi
  • 3,387
  • 7
  • 35
  • 57

4 Answers4

1

For example, like this:

a.ToString("0.00000000");
Hikiko
  • 309
  • 1
  • 2
  • 12
0

Try using

a.ToString("D8");

This should convert it to the correct format.

Edit:

As said in the comments, D is only used for integral types, thus not for decimals and such. Use

static void Main(string[] args)
{
    decimal d = (decimal)0.8537056986486486486486486486;
    Console.WriteLine(d.ToString("N8"));
    Console.ReadLine();
}

instead.

This will format it to 0.85370570

ThaMe90
  • 4,146
  • 5
  • 36
  • 63
0
 decimal a = 0.8537056986486486486486486486;    
 var v=  a.ToString("#.########");
zandi
  • 684
  • 5
  • 17
0

To perform this numerically you could use:

decimal a = (decimal) 0.8537056986486486486486486486;
String test = (Math.Truncate(100000000 * a) / 100000000).ToString();
Phil Applegate
  • 567
  • 2
  • 9
  • Instead of casting you could also add the [flag](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types#real-literals) `m` at the end of the number. – Flimtix Apr 08 '22 at 09:13