| | |
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 |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows





