Ask coding questions

← Back to all posts
How to print "1.00" in Java
L0k3d20uz4 (0)

I want my program to be able to find the cost depending on the user input and then display the cost in dollars form with 2 digits at the end. Right now, it shows "$4.0" (when I want it to show "$4.00"), "$5.5" (when I want it to show "$5.50") and "$7.0" (when I want it to show "$7.00").

How do I achieve this two-digit form?

Comments
hotnewtop
m4rus3ru (15)
NumberFormat nf = NumberFormat.getCurrencyInstance();
String bla = nf.format(moneyz);
L0k3d20uz4 (0)

@m4rus3ru says it can't find the getCurrencyInstance symbol. I imported NumberFormat and Currency

m4rus3ru (15)

import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;

L0k3d20uz4 (0)

@m4rus3ru
Main.java:22: error: cannot find symbol NumberFormat nf = new NumberFormat.getCurrencyInstance(); ^ symbol: class getCurrencyInstance location: class NumberFormat 1 error compiler exit status 1

m4rus3ru (15)

@L0k3d20uz4 Why did you add new to it?

L0k3d20uz4 (0)

@m4rus3ru oh, didn't realize sorry. I removed the new but it still does not work. I updated my post so you can see what I have right now.