In C#, using the double number 191.5 (example), I want to create a string that:

  • has exactly 10 characters
  • filled with 0 to the left
  • includes the dot character (.)
  • include exactly 1 decimal

My code :

strOut += SubString(10, string.Format("{0:0000000000}", 191.5));

The problem with my code is that :

  • it rounds (I don't want that)
  • it wipes the decimal

My code output :

0000000192

The output I need :

00000191.5
有帮助吗?

解决方案

This works just fine, don't need the substring.

var results = string.Format("{0:00000000.0}", 191.5));

其他提示

I believe

strOut += SubString(10, string.Format("{0:00000000.0}", 191.5));

does what you want. However, you don't really need the substring in this case unless you are sometimes expecting some much larger numbers and want to truncate them.

try this one

double num=192.5; 
strOut += SubString(10, string.Format("{0:#}", num));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top