How to print a float with 2 decimal places in Java?
You can use the printf
method, like so:
System.out.printf("%.2f", val);
In short, the %.2f
syntax tells Java to return your variable (val
) with 2 decimal places (.2
) in decimal representation of a floating-point number (f
) from the start of the format specifier (%
).
There are other conversion characters you can use besides f
:
d
: decimal integero
: octal integere
: floating-point in scientific notation
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
Format number to always show 2 decimal places
(Math.round(num * 100) / 100).toFixed(2);
Live Demo
var num1 = "1";document.getElementById('num1').innerHTML = (Math.round(num1 * 100) / 100).toFixed(2);
var num2 = "1.341";document.getElementById('num2').innerHTML = (Math.round(num2 * 100) / 100).toFixed(2);
var num3 = "1.345";document.getElementById('num3').innerHTML = (Math.round(num3 * 100) / 100).toFixed(2);
span { border: 1px solid #000; margin: 5px; padding: 5px;}
<span id="num1"></span><span id="num2"></span><span id="num3"></span>
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
Using String Format to show decimal up to 2 places or simple integer
An inelegant way would be:
var my = DoFormat(123.0);
With DoFormat
being something like:
public static string DoFormat( double myNumber )
{
var s = string.Format("{0:0.00}", myNumber);
if ( s.EndsWith("00") )
{
return ((int)myNumber).ToString();
}
else
{
return s;
}
}
Not elegant but working for me in similar situations in some projects.
How to print a double with two decimals in Android?
yourTextView.setText(String.format("Value of a: %.2f", a));
How to display a float with two decimal places?
You could use the string formatting operator for that:
>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
The result of the operator is a string, so you can store it in a variable, print etc.
Show a number to two decimal places
You can use number_format():
return number_format((float)$number, 2, '.', '');
Example:
$foo = "105";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00
This function returns a string.
How to print float to n decimal places including trailing 0s?
For Python versions in 2.6+ and 3.x
You can use the str.format
method. Examples:
>>> print('{0:.16f}'.format(1.6))
1.6000000000000001
>>> print('{0:.15f}'.format(1.6))
1.600000000000000
Note the 1
at the end of the first example is rounding error; it happens because exact representation of the decimal number 1.6 requires an infinite number binary digits. Since floating-point numbers have a finite number of bits, the number is rounded to a nearby, but not equal, value.
For Python versions prior to 2.6 (at least back to 2.0)
You can use the "modulo-formatting" syntax (this works for Python 2.6 and 2.7 too):
>>> print '%.16f' % 1.6
1.6000000000000001
>>> print '%.15f' % 1.6
1.600000000000000
Related Topics
Sql Merge Two Rows With Same Id But Different Column Values (Oracle)
Create a Query That Will Display Employee Name and Department Number
Sql Query to Select from 1 Hour Ago
Count All Records Per Day in a Specific Month
Insert Multiple Rows Without Repeating the "Insert into ..." Part of the Statement
T-Sql Query to Get the String Between Two Special Characters
How to Import CSV Data into a Table Without Knowing the Columns of the Csv
1052: Column 'Id' in Field List Is Ambiguous
Grouping But With Keeping All Non-Null Values
Sql Select Last N Rows, Sort Them Reversed
State Wise Data of Country and City
How to Get the Unmatched Records from Two Tables Using Joins
How to Connect MySQL Workbench to Running MySQL Inside Docker
How to Get Only One Row Against Each Id in MySQL
Selecting First and Last Values in a Group
Simulating group_concat MySQL Function in Microsoft SQL Server 2005