문제

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