Computer software has many real-world applications, such as providing an online marketplace for goods and services. Modern languages acknowledge these real-world applications using built-in techniques for tackling common tasks. For example, Java offers a method for converting between regular number types and currency number types. This is very convenient for programmers who develop online storefronts or accounting software. If your program deals with currency, familiarize yourself with the Java BigDecimal class.
Things You'll Need
- Java Development Kit and Netbeans Bundle (see Resource)
Load the Java development environment by clicking on the NetBeans icon. Navigate to "File/New Project" in the top menu and choose "Java Application." A new source code file appears in the main editor window.
Import Java Math, Text, and Util libaries by writing these lines of code at the top of the source code file:
Define a BigDecimal object by writing the next line of code between the curly brackets of the main function:
BigDecimal cost = new BigDecimal("1120304.73");
Create a NumberFormat object named "Dollars" and assign the U.S. currency type to it. To do this, write this line of code:
NumberFormat dollars = NumberFormat.getCurrencyInstance(Locale.US);
Create a string of text and assign the BigDecimal variable "Cost" to it. Since the cost variable needs to be properly formatted to the U.S. currency type, you can run the format function at the same time you assign it to the string, like this:
String s = dollars.format(cost);
Print out the string like this:
Run the program by pressing "F6." The output looks like this:
How to Use Excel's Dollar Function
Excel's DOLLAR function converts a numeric value to a text value and applies a currency symbol. This function's name and the currency...
How to Install Roof Brackets
Roof brackets or jacks are handy little devices that are used by roofers as scaffolding when installing a new roof. They are...
How to Earn Money By Developing ANDROID JAVA Applications
In the world of competition between smartphones like Blackberry, iPhone and Droid, application developers have a huge potential to become rich in...
How to Convert Short to Int in Java
The integer data type in Java is a whole-number data type that consumes 32 bits of memory and can hold values between...