Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Erlang

Idiom #23 Convert real number to string with 2 decimal places

Given a real number x, create its string representation s with 2 decimal digits following the dot.

S = io_lib:format("~.2f", [X]).

S will be an iolist(), technically not a string() but it's good enough for most purposes and you can always use iolist_to_binary/1
#include <stdio.h>
sprintf(s, "%.2f", x);

New implementation...
< >
programming-idioms.org