Pergunta

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
Foi útil?

Solução

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

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

Outras dicas

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));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top