Format Float Value with 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

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 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)
}

Store float with exactly 2 decimal places in C++

Your decision to store monetary amounts as integer number of cents is a wise one, because floating-point data types (such as float or double) are generally deemed unsuitable for dealing with money.

Also, you were almost there by finding std::setprecision. However, it needs to be combined with std::fixed to have the expected effect (because std::setprecision means different things depending on which format option is used: the default, scientific or fixed).

Finally, to store the formatting result in an std::string instead of directly printing it to the console, you can use a string-based output stream std::ostringstream. Here is an example:

#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

std::string cents_to_dollars_string(const int cents)
{
static constexpr double cents_per_dollar{ 100. };
static constexpr int decimal_places{ 2 };

std::ostringstream oss;
oss << std::fixed << std::setprecision(decimal_places) << cents / cents_per_dollar;
return oss.str();
}

int main()
{
const int balance_in_cents{ -420 };
const std::string balance_in_dollars{ cents_to_dollars_string(balance_in_cents) };
std::cout << "Your balance is " << balance_in_dollars << '\n';
}

Here, we first define the function cents_to_dollars_string, which takes the amount in cents as an int and returns an std::string containing the formatted amount of dollars. Then, in main we call this function to convert an amount (in cents) stored in an int variable balance_in_cents to a string and store it into an std::string variable balance_in_dollars. Finally, we print the balance_in_dollars variable to the console.

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

Round up double to 2 decimal places

Use a format string to round up to two decimal places and convert the double to a String:

let currentRatio = Double (rxCurrentTextField.text!)! / Double (txCurrentTextField.text!)!
railRatioLabelField.text! = String(format: "%.2f", currentRatio)

Example:

let myDouble = 3.141
let doubleStr = String(format: "%.2f", myDouble) // "3.14"

If you want to round up your last decimal place, you could do something like this (thanks Phoen1xUK):

let myDouble = 3.141
let doubleStr = String(format: "%.2f", ceil(myDouble*100)/100) // "3.15"

Format Float to n decimal places

You may also pass the float value, and use:

String.format("%.2f", floatValue);

Documentation

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.

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


Related Topics



Leave a reply



Submit