Showing posts with label JOptionPane. Show all posts
Showing posts with label JOptionPane. Show all posts

Tuesday, February 12, 2013

Tutorial 13 : Java Formatting Double Value


Java Tutorial For Beginner, Tutorial 13 : Formatting Double Value

To format double value to 2 decimal format, use java.text.DecimalFormat class to format the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator.

In this example, to format to decimat format i use the following statements;

Step 1; Use the java.text.* to use java.text.DecimalFormat class
import javax.swing.JOptionPane;

Step 2: Create DecimalFormat object called dFormat, use to format 2 decimal point by using the pattern "#.##"

DecimalFormat dformat= new DecimalFormat("#.##");

Step 3 : Finally, use format() method to format the double value. It accepts double value as argument and returns a string.

dformat.format(celsius)

Code below :


// Celsius to Fahrenheit Converter Example
// How to format double value;
// Watch output without formatting value;

import javax.swing.JOptionPane;

// add this
import java.text.*;

public class Fahrenheit{
public static void main(String[] args){

String number = JOptionPane.showInputDialog(null, "Enter fahrenheit value : " , "Fahrenheit to Celsius Converter", JOptionPane.QUESTION_MESSAGE);

//convert string to number
double fahrenheit = Double.parseDouble(number);

double celsius = (5.0/9)*(fahrenheit- 32);

// Add this to format the double value
DecimalFormat dformat= new DecimalFormat("#.##");


// To produce, 2 decimal point output
JOptionPane.showMessageDialog(null, "Fahrenheit value is " + fahrenheit + "\n" + "Celcius value is " + dformat.format(celsius) ,
"Fahrenheit to Celsius", JOptionPane.INFORMATION_MESSAGE);

}

}

Tutorial 12 : Fahrenheit To Celsius Example JOption Input

Fahrenheit To Celsius Example.

Step 1 : I use JOptionPane.showInputDialog to get the fahrenheit reading

Step 2 : calculate celsius using the following calculate

double celsius = (5.0/9)*(fahrenheit- 32);

Step 3 : Show the result using JOptionPane.showMessageDialog

Code below :


// Celsius to Fahrenheit Converter Example


import javax.swing.JOptionPane;


public class Fahrenheit{
public static void main(String[] args){

String number = JOptionPane.showInputDialog(null, "Enter fahrenheit value : " , "Fahrenheit to Celsius Converter", JOptionPane.QUESTION_MESSAGE);

//convert string to number
double fahrenheit = Double.parseDouble(number);

double celsius = (5.0/9)*(fahrenheit- 32);


JOptionPane.showMessageDialog(null, "Fahrenheit value is " + fahrenheit + "\n" + "Celcius value is " + celsius ,
"Fahrenheit to Celsius", JOptionPane.INFORMATION_MESSAGE);

}

}

Tutorial 11 : Entering input JOptionPane, Addition Example

Java Tutorials For Beginners, Tutorial 11 : Entering input JOptionPane, Addition Example.

Another way to get input is to use JOptionPane class. Previously I was using Scanner class.

To use it, add the import javax.swingJOptionPane ; statement .

To get input we use, the following code ,

JOptionPane.showInputDialog(null, "Enter 1st number", "Input Example", JOptionPane.QUESTION_MESSAGE);

The 1st argument as we use null, second to prompt user to enter the 1st number, the third is the title of input box and 4th causes icon ? question mark to appear.

To show the output, we use the following statement:

JOptionPane.showMessageDialog(null, "Total sum is : " + (x+y), "Input Example", JOptionPane.INFORMATION_MESSAGE );

The first normally we put null, second is the output, the third is the title of the dialog box and the 4th show the i icon in the message box.



Code


// Entering input using JOptionPane

import javax.swing.JOptionPane;

public class InputDialog{
public static void main(String[] args){

String number1 = JOptionPane.showInputDialog(null, "Enter 1st number", "Input Example", JOptionPane.QUESTION_MESSAGE);

//CONVERT STRING TO NUMBER

int x = Integer.parseInt(number1);

String number2 = JOptionPane.showInputDialog(null, "Enter 2nd number", "Input Example", JOptionPane.QUESTION_MESSAGE);

//CONVERT STRING TO NUMBER

int y = Integer.parseInt(number2);

//Show the output, summation

JOptionPane.showMessageDialog(null, "Total sum is : " + (x+y), "Input Example", JOptionPane.INFORMATION_MESSAGE );

}
}