#include <stdio.h>
printf("value: %.5f", 0.02);
Note: The usage of a BigDecimal-like class or a fixedpoint class (with a scale factor of 10^x) instead of double or float would be more appropriate here because with that you would be able to reduce error coming from floating point calculations to zero. Serious banking applications often make use of BigDecimal libraries.
EDIT: Some useful links:
- Depending on your problem using a fixed-point class instead of double/float may be enough:
Fixed-point arithmetic[
^]. If a fixed number of digits in the fraction isn't suitable for you then fixed-point isn't a good solution.
- If fixed-point arithmetic isn't enough then you should use a library that implements the decimal32, decimal64, or decimal128 floating-point types listed in a table on this page:
IEEE floating point[
^]. decimalXXX types are also floating point types like binary32(float) and binary(64)double but they use 10 as the base of the exponent instead of 2 so they can represent all decimal numbers accurately within a specified range. Unlike with fixed point types you can move the decimal point as you can with float/double.
- If the range of the decimal implementation isn't enough then you can choose a BigDecimal library that can store arbitrarily large decimal numbers accurately. Banking applications use this solution to make calculations with the amount of money on each account.