Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Python
- Python
- Python
- Python
- C
- C++
- C++
- C++
- C#
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Haskell
- JS
- Java
- Java
- Java
- Java
- Java
- Kotlin
- Lisp
- Lua
- PHP
- Pascal
- Perl
- Ruby
- Rust
s = format(x, '.2f')
s = '%.2f' % x
std::array<char, 23> buffer;
std::string s{};
if (auto [ptr, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), x, std::chars_format::fixed, 2); ec == std::errc{}) {
s = std::string(buffer.data(), ptr);
} else {
s = std::make_error_code(ec).message();
}
string s = $"{x:F2}";
s = Float.to_string(x, decimals: 2)
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
write (unit=s,fmt="(F20.2)") x
s <- showFFloat (Just 2) x ""
import static java.math.RoundingMode.HALF_UP;
import java.math.BigDecimal;
import java.math.MathContext;
MathContext m = new MathContext(3, HALF_UP);
BigDecimal d = new BigDecimal(x, m);
String s = d.toPlainString();
String s = "%.2f".formatted(x);
String s = String.format("%.2f", x);
import static java.math.RoundingMode.HALF_UP;
import static java.text.NumberFormat.getNumberInstance;
import java.text.NumberFormat;
NumberFormat f = getNumberInstance();
f.setRoundingMode(HALF_UP);
f.setMaximumFractionDigits(2);
String s = f.format(x);
(setf s (format nil "~$" x))
$s = sprintf('%.2f', $x);
$s = sprintf "%.2f", $x;
s = "%.2f" % x