Search code examples
c#string-formatting

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


Solution

  • 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.