Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I display a double to 2 decimal points? [duplicate]

Tags:

flutter

dart

How I can display or keep double values to 2 decimal points?

textContent: 'Total Payable: ' + '€'+finalPrice.toString(),

I need to know how to ensure that

double finalPrice; 

that finalPrice is always displayed to 2 decimal points. Whether it is converted to a string or not.

I've tried everything and called my MP.

double finalPrice = 0.00;

I noticed that initialising it as 0.00 seems to do something, but I need something a bit more solid.

The expected result is the client should pay for what they've purchased. The actual result is we are charging the customer a bit more than what they've purchased

like image 583
Kiro777 Avatar asked Aug 22 '19 16:08

Kiro777


People also ask

How do you make a double show with two decimal places?

Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places.

How do you double a value to two decimal places in Excel?

By using a button: Select the cells that you want to format. On the Home tab, click Increase Decimal or Decrease Decimal to show more or fewer digits after the decimal point.

How do you print numbers to two decimal places?

we now see that the format specifier "%. 2f" tells the printf method to print a floating point value (the double, x, in this case) with 2 decimal places.

How do you print float up to 2 decimal places?

format() with “{:. 2f}” as string and float as a number. Call print and it will print the float with 2 decimal places.


1 Answers

double d = 1.5124;
String number = d.toStringAsFixed(2); // 1.51

Even if you have

double d = 1.51 // number: 1.51
double d = 1.5 // number: 1.50
double d = 1 // number = 1.00

You can see you will always have 2 decimal places with toStringAsFixed(2).

like image 73
CopsOnRoad Avatar answered Sep 30 '22 08:09

CopsOnRoad