简体   繁体   中英

C# double type formatting without decimal point and 2 digits

I need to format a double as a string with 15 chars, padded left with zeros (0). The string must contain the 2 digits but not the decimal point even thousand separator.

I did it using the below code, but I wonder if there is a better way to do using only format string.

double value = 15.85;
CultureInfo info = CultureInfo.GetCultureInfo("en-US");
string s = (value).ToString("F2", info).Replace(".", string.Empty).PadLeft(15, "0"[0]);
//will output 000000000001585
double value = 15.85;

string s1 = String.Format("{0:000000000000000}", value); // 000000000000015
string s2 = String.Format("{0:0000.00}", value);         // 0015.85

没有一种方法可以显示数字格式的不带小数点的小数位数,但是有一种比字符串替换/填充更简单的方法来删除小数点-只需将值乘以100:

(value*100).ToString("000000000000000");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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