Logo

Programming-Idioms

  • VB
  • Obj-C
  • Ruby
  • Pascal

Idiom #65 Format decimal number

From the real value x in [0,1], create its percentage string representation s with one digit after decimal point. E.g. 0.15625 -> "15.6%"

SysUtils
s :=format('%.1f%%', [100.0*x]);  
s = "%.1f%%" % (100 * x)

Note that you need to use %% (double %) inside a format string to produce a literal percent sign
#include <stdio.h>
char s[7];
sprintf(s, "%.2lf%%", x * 100);

New implementation...