简体   繁体   English

将double转换为字符串,保持格式 - C#

[英]Convert double to string, keep format - C#

Hi all. 大家好。 I have a double number (Ex: 0.000006). 我有一个双号(例如:0.000006)。 I want convert it to string type. 我想将它转换为字符串类型。 But result is "6E-06". 但结果是“6E-06”。 I dont want it, i want 0.000006".Thanks you so much 我不想要它,我想要0.000006“。非常感谢你

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

I don't know many number after "." 我不知道“。”之后的多少数字。 character 字符

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: 很简单,如果你想要显示一个与它看起来完全一样的数字,我们可以将其转换为decimal并使用默认的ToString()如下所示:

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

Please check this article and find the format which suits your needs: http://www.csharp-examples.net/string-format-double/ 请查看本文并找到适合您需求的格式: http//www.csharp-examples.net/string-format-double/

Looks like this one is good enough: 看起来这个很好:

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

Use String.Format() with the format specifier . 使用带有格式说明符的 String.Format()

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

Try with the Roundtrip 'R' format specifier : 尝试使用Roundtrip'R'格式说明符

double a = 0.000006;
string resultString = a.ToString("R"); 
            Double Rate_USD = Convert.ToDouble(txtRateUsd.Text);
            string Rate_USD = txtRateUsd.Text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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