[英]Convert double to string (C#)
Convert a double from 12.45
to a string like 00001245
(length of the string = 8). 将double值从
12.45
转换为类似00001245
的字符串(字符串的长度= 8)。
How could I do that? 我该怎么办?
String.Format("{0:D8}", (int)(12.45 * 100));
You can do something like: 您可以执行以下操作:
Multiply it with 100, to get rid of the .
将其乘以100即可消除
.
Cast it to an int
, to get rid of leftover decimals. 将其强制转换为
int
,以消除剩余的小数。
Convert it to a string
with ToString()
使用
ToString()
将其转换为string
And pad it on the left with a totalsize of 8 with '0'
并在左侧将其总大小为8并加上
'0'
double d = 12.45;
string value = ((int)(d * 100)).ToString().PadLeft(8, '0');
If your double always has 2 digits after comma, looks like you need to multiply with 100
and use String.PadLeft
method until you get 8 length of string. 如果您的双
String.PadLeft
数始终在逗号后有2位数字,则表明您需要乘以100
并使用String.PadLeft
方法,直到得到8个字符串长度。
double d = 12.45;
Console.WriteLine((d * 100).ToString().PadLeft(8, '0'));
Output will be; 输出将是;
00001245
也可以这样做:
String.Format("{0:00000000}", 12.45 * 100)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.