Logo

Programming-Idioms

  • Smalltalk
  • C#
  • C

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%"

#include <stdio.h>
char s[7];
sprintf(s, "%.2lf%%", x * 100);
string s = $"{x:p1}";
#include <format>
auto s = std::format("{.1f}%", x * 100);

Based on C++20 <format>.

New implementation...