fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. var hp = new HP(34, 58);
  8.  
  9. Console.WriteLine(string.Format("{0}", hp));
  10. Console.WriteLine(string.Format("{0:P}", hp));
  11. }
  12. }
  13.  
  14. class HP : IFormattable
  15. {
  16. private int _maxHp;
  17. private int _hp;
  18.  
  19. public HP (int hp, int maxHp)
  20. {
  21. _hp = hp;
  22. _maxHp = maxHp;
  23. }
  24.  
  25. public override String ToString() { return ToString(null, null); }
  26.  
  27. public string ToString(string format, IFormatProvider fp)
  28. {
  29. if (format == null)
  30. {
  31. return string.Format("{0}/{1}", _hp, _maxHp);
  32. }
  33.  
  34. if (format == "P")
  35. {
  36. return string.Format("{0:P}", (float)_hp / (float)_maxHp);
  37. }
  38.  
  39. throw new FormatException(string.Format("Invalid format string: '{0}'.", format));
  40. }
  41. }
Success #stdin #stdout 0.03s 33896KB
stdin
Standard input is empty
stdout
34/58
58.62 %