| | |
Convert float to string gets error
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 149
Reputation:
Solved Threads: 7
Hello
I'm new to java & I am trying to convert a float to a string in my simple program. But I get an error on that line. Can you tell me why or what I need to do to fix it?
Maybe I haven't imported the right things or sumthing?
I'm new to java & I am trying to convert a float to a string in my simple program. But I get an error on that line. Can you tell me why or what I need to do to fix it?
Maybe I haven't imported the right things or sumthing?
java Syntax (Toggle Plain Text)
import java.io.IOException; import java.util.Scanner; // must have this to take in input (cin) public class Main { public Main() { } public static void main(String[] args) throws IOException { // Create scanner variable (cin variable) Scanner scan = new Scanner(System.in); System.out.println("\nWelcome to the Interest Calculator"); char decision = 'y'; while (decision == 'y' || decision == 'Y') // cin >> decision { System.out .println("\nEnter loan amount: "); float loan = scan.nextFloat(); System.out.println("Enter interest rate: "); float rate = scan.nextFloat(); System.out.println("\nLoan amount: $" + loan); System.out.println("Interest rate: " + (rate*100) + "%"); float interest = (loan*rate); System.out.println("Interest: $" + interest); System.out.println("Continue? (y/n): "); decision = (char)System.in.read(); } } public String alter(float n) { /// Pre: variable n must be of type float /// Post: return loan amount as a string with commas inserted String number = Float.toString(number); // ERROR HERE convert float to string; number.length(); return number; } }
1
#2 Oct 10th, 2009
java Syntax (Toggle Plain Text)
String number = Float.toString(number);
What I would do is this:
java Syntax (Toggle Plain Text)
String number = new Float(n).toString();
Here I create a Float object from my float primitive, then convert it to a String. You have tried to call the toString method statically and then pass the variable number instead of n.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. 1
#3 Oct 11th, 2009
I always use the static methods of the String Object.
You can also use that for various other primitive types like int, long, char, double etc.
Java Syntax (Toggle Plain Text)
String number = String.valueOf(n);
You can also use that for various other primitive types like int, long, char, double etc.
![]() |
Similar Threads
- Convert vector<string> to vector<double> (C++)
- Cannot implicitly convert type 'string' to 'bool' (ASP.NET)
- Convert integer to string (C++)
- Conversion From Float to String (C++)
- convert int to string (C)
- How do I convert a vector to a String array ? (Java)
Other Threads in the Java Forum
- Previous Thread: Illegal Start of type error
- Next Thread: constructors?
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation binary blackberry block bluetooth character chat class classes client code component consumer database desktop developmenthelp draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing template textfields threads time title tree tutorial-sample update windows working





