Nullbyte
Nullbyte

Reputation: 251

Format string % without multiply the value by 100

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:}?

Upvotes: 4

Views: 2692

Answers (4)

j-j
j-j

Reputation: 93

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

intPercent.ToString(@"0\%");

Upvotes: 0

Hossain Muctadir
Hossain Muctadir

Reputation: 3626

What about

var showString = userInput + "%";

Then display showString wherever you need to show.

Upvotes: 1

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

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.

Upvotes: 4

Szymon
Szymon

Reputation: 43023

Can't you just append %:

userEnteredNumber.ToString() + "%";

Upvotes: 3

Related Questions