Formatting a Float to 2 Decimal Places

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 integer
  • o: octal integer
  • e: floating-point in scientific notation

Formatting a float to 2 decimal places

You can pass the format in to the ToString method, e.g.:

myFloatVariable.ToString("0.00"); //2dp Number

myFloatVariable.ToString("n2"); // 2dp Number

myFloatVariable.ToString("c2"); // 2dp currency

Standard Number Format Strings

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.

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>

Format number to 2 decimal places

You want to use the TRUNCATE command.

https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_truncate

Format float value with 2 decimal places

You can use standard string formatting specifiers to round to an arbitrary number of decimal places. Specifically %.nf where n is the number of decimal places you require:

let twoDecimalPlaces = String(format: "%.2f", 10.426123)

Assuming you want to display the number on each of the l* labels:

@IBAction func Berechnen(sender: AnyObject) {

var Zahl = (txt.text as NSString).floatValue

l5.text = String(format: "%.2f", (Zahl / 95) * 100)
l10.text = String(format: "%.2f", (Zahl / 90) * 100)
l15.text = String(format: "%.2f", (Zahl / 85) * 100)
l20.text = String(format: "%.2f", (Zahl / 80) * 100)
l25.text = String(format: "%.2f", (Zahl / 75) * 100)
l30.text = String(format: "%.2f", (Zahl / 70) * 100)
l35.text = String(format: "%.2f", (Zahl / 65) * 100)
l40.text = String(format: "%.2f", (Zahl / 60) * 100)
}

JavaScript displaying a float to 2 decimal places


float_num.toFixed(2);

Note:toFixed() will round or pad with zeros if necessary to meet the specified length.

Formatting a number with exactly two decimals in JavaScript

To format a number using fixed-point notation, you can simply use the toFixed method:

(10.8).toFixed(2); // "10.80"

var num = 2.4;
alert(num.toFixed(2)); // "2.40"

Note that toFixed() returns a string.

IMPORTANT: Note that toFixed does not round 90% of the time, it will return the rounded value, but for many cases, it doesn't work.

For instance:

2.005.toFixed(2) === "2.00"

UPDATE:

Nowadays, you can use the Intl.NumberFormat constructor. It's part of the ECMAScript Internationalization API Specification (ECMA402). It has pretty good browser support, including even IE11, and it is fully supported in Node.js.