CSE 110 Lab 3: Pre-lab: Read the following pre-lab material before your lab sess
ID: 3786627 • Letter: C
Question
CSE 110 Lab 3: Pre-lab: Read the following pre-lab material before your lab session. References from the textbook: String class and its methods DecimalFormat, and NumberFormat classes their methods Pre-lab Questions: Answer these questions before your lab session. 1. Write a line of code that declares a new string variable called muffin and initializes it to hold the string "Muffins are delicious!" 2. What is the difference between DecimalFormat and NumberFormat? 3. Write code that declares a DecimalFormat variable named dec that is set up to show numbers two decimal places.Explanation / Answer
1)Code statement:
String muffin="Muffins aredelicious"
=====================================================================================
2)To show how your numbers are formatted,use "DecimalFormat".And to show the way most numbers are represented in my current locale,use NumberFormat
=====================================================================================
3)
import java.text.DecimalFormat;
public class decimal {
private static double number = 45.678;
public static void main(String[] args) {
DecimalFormat dec = new DecimalFormat("###.##");
System.out.println(dec.format(number));
}
}
Output:
akshay@akshay-Inspiron-3537:~$ javac decimal.java
akshay@akshay-Inspiron-3537:~$ java decimal
45.68
==========================================================================