Example of a Java formatted numeric value to a currency format _java

Source: Internet
Author: User
Tags numeric value

Format a value, such as 123456789.123, that you want to display as "$123,456,789.123". To complete the requirement, you can format it with the Java.text.NumberFormat class

The NumberFormat class provides an encapsulation of numeric formats. In JDK, you typically use the NUMBERFORMT subclass----Java.text.DecimalFormat to complete this function. The most common constructors for this class are:

Public DecimalFormat (String pattern)

Where the parameter pattern represents the passed-in format string

Code:

Copy Code code as follows:

Import Java.text.DecimalFormat;
Import Java.text.NumberFormat;
public class NumberFormat
{
public static void Main (string[] args)
{
NumberFormat NF = new DecimalFormat ("$,###.##");
String teststr = Nf.format (123456789.123);
System.out.println (TESTSTR);
}
}

DecimalFormat Features:

Accept the corresponding format string and format the parts in the numeric values. #表示阿拉伯数字

In a format string, except for the part that has a representative meaning, other parts, such as $, appear as if they were

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.