Futureman
Futureman

Reputation: 309

Convert double to string, keep format - C#

Hi all. I have a double number (Ex: 0.000006). I want convert it to string type. But result is "6E-06". I dont want it, i want 0.000006".Thanks you so much

double a = 0.000006; 
string resultString = a.toString();

I don't know many number after "." character

Upvotes: 3

Views: 23161

Answers (5)

Arvind Upadhyay.
Arvind Upadhyay.

Reputation: 1

            Double Rate_USD = Convert.ToDouble(txtRateUsd.Text);
            string Rate_USD = txtRateUsd.Text;

Upvotes: 0

King King
King King

Reputation: 63357

It's simple that if you want to show a number as exactly as what it looks, we can cast it to decimal and use the default ToString() like this:

var s = ((decimal)yourNumber).ToString();
//if yourNumber = 0.00000000000000000000000006
//just append the M after it:
var s = (0.00000000000000000000000006M).ToString();

Upvotes: 6

Gianni B.
Gianni B.

Reputation: 2731

Try with the Roundtrip 'R' format specifier:

double a = 0.000006;
string resultString = a.ToString("R"); 

Upvotes: 0

user2742064
user2742064

Reputation:

Please check this article and find the format which suits your needs: http://www.csharp-examples.net/string-format-double/

Looks like this one is good enough:

String.Format("{0:0.00}", 123.4567); 

Upvotes: 4

Rohit
Rohit

Reputation: 10286

Use String.Format() with the format specifier.

double a = 0.000006;
string formatted = String.Format("{0:F6}", a);

Upvotes: 3

Related Questions