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 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...
How to Write Fifteen Thousand Dollars and Ten Cents in Check Format
When you write a check, it is your responsibility to make sure the amount is accurate, and cannot be altered. You must...
How to Use Excel's Dollar Function
Examine the way DOLLAR formats the result. The currency symbol will be added to the beginning of the string and a comma...
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 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 Convert Dollars to Euros in Google
In order to convert dollars to euros, go to Google's text entry area and type "ten dollars in euros" to get the...