25.0k views
4 votes
Using the NumberFormat class, write code that will create a double called cv, assign it a value of 18.7713, and then convert it to a String rounded off to the nearest hundredth. Assure that at least 1 decimal place is printed. Print the String.

1 Answer

4 votes

Answer:

// Program to convert rounded decimal to string

// This program segment is written in Java programming language

// Comments are used for explanatory purpose

// Program starts here

import java.text.D ecimalFormat;

public class RoundNumbers

{

public static void main(String[] args) {

// Declare number and assign value to it

double cv = 18.7713;

// Call decimalformat method

DecimalFormat commaType = new DecimalFormat("#,###.###");

//Convert cv to string

String myPi = commaFormat.format(cv);

// Print result

System.out.println(commaType.format(cv));

}

}

// End of Program

User Daniel Kvist
by
4.1k points