146 percent Russian
146 percent Russian

Reputation: 2096

Formatting double to String

I make such formatting:

 str += String.Format("{0,-25}", doubl);

I need my numbers to be divided by spaces, that's why I use {0,-25}.

But sometimes I get numbers like 2.15083096934631E-05

How to get traditional numbers like 0,000021508 , without "E-05" ?

The value of doubl is like 0.000021436573129839587

Upvotes: 1

Views: 1353

Answers (4)

Rusty Nail
Rusty Nail

Reputation: 2710

All the Answers prior to mine were fine but for me, I had a very ugly problem. My 'Answer' would be filled by Zero's and it made the answer very bulky if there was only 5 Zeros needed. I came up with this:

double Q = ((1000 * 1000) * (2 * 2));
double Answer = ((Q * 199) / 2);
this.AnswerTBox1.Text = String.Format("{0:F" + Answer.ToString().Length + "}", Answer);

This tidy's up the 'Uglyness' of unnecessary Zeros.

All the Best

Chris

Upvotes: 0

V4Vendetta
V4Vendetta

Reputation: 38200

You can format the output by passing in F or N to the ToString(), to represent more decimal points i think you will have to specify something like N15 (find the suitable limit you need depending on your values) but the issue here is trailing 0 after your actual number

[double value].ToString("N15") // this should output without the exponent sign

To avoid trailing zeros you just need to Trim

[double value].ToString("N15").TrimEnd('0');

Upvotes: 0

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79889

Use the The "0" Custom Specifier:

str = doubl1.ToString("0.0000000");

Upvotes: 2

KV Prajapati
KV Prajapati

Reputation: 94625

Try double.ToString("format") (F - Fixed-point)

str=doubleValue.ToString("F10");

Upvotes: 2

Related Questions