In Java, you can use the NumberFormat
class from the java.text
package to format numbers as currency values.
Here is an example of how to use the NumberFormat
class to format a number as a currency value in Java:
import java.text.NumberFormat; import java.util.Locale; public class Main { public static void main(String[] args) { // Create a NumberFormat instance for the US locale NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US); // Format a number as a currency value double amount = 123.45; String formattedAmount = formatter.format(amount); // Print the formatted currency value System.out.println(formattedAmount); // Output: $123.45 } }Sour.www:eclautturi.com
In this example, we create a NumberFormat
instance for the US locale using the NumberFormat.getCurrencyInstance
method. We then call the format
method on the NumberFormat
instance to format the number 123.45
as a currency value. The format
method returns a string representation of the formatted currency value.
You can also use the NumberFormat
class to format currency values for other locales. For example, you can use the Locale.FRANCE
constant to create a NumberFormat
instance for the French locale, or the `Locale.