質問

I need to format the string as % but the user will enter the number already as %

i.e user enter 10, I need to show 10%

I tried {0:P} and {0:0%} but it always multiply the user number by 100

How can I simply add "%" to the input number without multiply it by 100 in the format {0:}?

役に立ちましたか?

解決

You have two real options, add the % by hand

String.Format("Example: {0}%", userValue);

or devide the user number by 100 before you display it.

String.Format("Example: {0:P}", userValue / 100.0); //don't just do "100" or you will get errors from integer division if userValue is a int.

他のヒント

Can't you just append %:

userEnteredNumber.ToString() + "%";

What about

var showString = userInput + "%";

Then display showString wherever you need to show.

We use this format for formatting int value to percent without multipying by 100.

intPercent.ToString(@"0\%");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top