# One Decimal Place

• August 21st, 2012, 10:20 PM
Akirien
One Decimal Place
Hello there... I want to convert the output to one decimal place but what I got is "Cannot format given Object as a Number". Can anyone help me out? Thank you :)

Code :

```//Assignment 1 Question 2   import java.text.*;     public class darjahCelcius   {   public static void main(String[] args)   {     System.out.println("Celsius\t\tFahrenheit\t|\tFahrenheit\t\tCelsius");   System.out.println("----------------------------------------------------------");   double celsius = 40; double fahrenheit = 120;   NumberFormat formatter = new DecimalFormat("#0.0");   for (int i = 1; i <= 10; celsius--, fahrenheit -= 10, i++)   {   System.out.println(formatter.format(celsius + "\t\t" +celsiusToFahrenheit(celsius) + "\t\t|\t" + fahrenheit + "\t\t\t" + fahrenheitToCelsius(fahrenheit)));   }   }   public static double celsiusToFahrenheit(double celsius)   { return (9.0 / 5.0) * celsius + 32; }   public static double fahrenheitToCelsius(double fahrenheit)   { return (5.0 / 9) * (fahrenheit - 32); }   }```
• August 21st, 2012, 10:26 PM
curmudgeon
Re: One Decimal Place
The error message is telling you exactly what is wrong. You use a DecimalFormatter to format a number period, but you're trying to pass it a whole complex String complete with several numbers and tabs(?). Instead why not simply pass it the number that you want to format and use its returned String in your output? You can use the formatter as many times as you want if you want to format several numbers, but again, only pass into it a single number.
• August 21st, 2012, 10:33 PM
Akirien
Re: One Decimal Place
Thanks for your reply.. But can you teach me how to do that?
• August 21st, 2012, 10:42 PM
curmudgeon
Re: One Decimal Place
Quote:

Originally Posted by Akirien
Thanks for your reply.. But can you teach me how to do that?

I'm trying. Again, what you're doing is creating a DecimalFormat object called formatter, and you're trying to format a non-numeric String. What you've got is:

Code :

`String myResultString = formatter.format( this huge complex String with numbers and tabs and what not).`

What you need to do instead is

Code :

```String myResultString = "Some complex String " + formatter.format(/*some number goes here*/) + " more String here " + formatter.format(/*another number goes here*/); // etc....```

Clear as Mudd?
• August 21st, 2012, 11:09 PM
Akirien
Re: One Decimal Place
thanks!!! got it...hehe.. thanx a lot.. :)
• August 22nd, 2012, 12:11 AM
curmudgeon
Re: One Decimal Place
You're welcome!