C language how to format a double to 2 digits after the decimal point?
The complete list
Integer display
%d print as decimal integer
%6d print as decimal integer, at least 6 characters wide
%f print as floating point
%6f print as floating point, at least 6 characters wide
%.2f print as floating point, 2 characters after decimal point
With the required modification (scanf("%.2f",&x);
in the last entry) will solve your problem
Leave only two decimal places after the dot
string.Format
is your friend.
String.Format("{0:0.00}", 123.4567); // "123.46"
How do I display a decimal value to 2 decimal places?
decimalVar.ToString("#.##"); // returns ".5" when decimalVar == 0.5m
or
decimalVar.ToString("0.##"); // returns "0.5" when decimalVar == 0.5m
or
decimalVar.ToString("0.00"); // returns "0.50" when decimalVar == 0.5m
Printing the correct number of decimal points with cout
With <iomanip>
, you can use std::fixed
and std::setprecision
Here is an example
#include <iostream>
#include <iomanip>
int main()
{
double d = 122.345;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << d;
}
And you will get output
122.34
Round floating point:Print exactly two digits after the decimal point rounded to the nearest decimal place
C or C++ don't care about what you need.
12.555 is not a binary floating point number. Binary floating point numbers are all integers multiplied by or divided by a power of two. 12.555 is not such a number. So when you write 12.555 then what you get is the floating-point number nearest to 12.555. Which may be smaller or larger than 12.555 and will be correctly rounded to 12.55 or correctly rounded to 12.56.
Calculate x * 1000 and round (x * 1000), which will give 12555. If x * 1000 is very close to round (x * 1000) and the last digit of round (x * 1000) is odd then increase it by 1. Divide by 10, round again, divide by 100.
Round double in two decimal places in C#?
This works:
inputValue = Math.Round(inputValue, 2);
Related Topics
Is There a Standard Way of Moving a Range into a Vector
How to Pass Derived Classes by Reference to a Function Taking Base Class as a Parameter
Eclipse C++:"Program "G++" Not Found in Path"
"Step Over" When Debugging Multithreaded Programs in Visual Studio
C++ Read File Line by Line Then Split Each Line Using the Delimiter
How to Resize a 2D Vector of Objects Given the Width and Height
In C/C++, Is Char* Arrayname[][] a Pointer to a Pointer to a Pointer or a Pointer to a Pointer
What Is the Ndebug Preprocessor MACro Used for (On Different Platforms)
Can You Have a Triple Minus Signs in C Programming? What Does It Mean
Can Std::Vector Emplace_Back Copy Construct from an Element of the Vector Itself
Why Was Pair Range Access Removed from C++11
Error: Class Has Not Been Declared Despite Header Inclusion, and the Code Compiling Fine Elsewhere
Erase/Remove Contents from the Map (Or Any Other Stl Container) While Iterating It
C++: How to Get Fprintf Results as a Std::String W/O Sprintf
Std::Forward_List and Std::Forward_List::Push_Back