954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Convert float to string

Here is the output that I should be receiving:

Please enter the name on the check (First Last): John Doe
Please enter the amount for the check (as a float): 123.45
 ---------------------------
Pay to : John Doe           $123.45 
one hundred twenty three dollars and  45 cents


Now, what I am having problems with is turning the $123.45 into one hundred twenty three dollars and 45 cents.

Thanks for your help,
- Aepexx

Aepexx
Newbie Poster
12 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Post the code you have to start with. Ask specific questions about what you are having trouble with and what you have tried so far.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

have you written anything yet? show us what you've got so far, don't wait until we write it for you

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Here is what I have written so far:

import java.util.Scanner;
public class Asn8
{
	public static void main(String args[])
	{
		Scanner kb = new Scanner(System.in);
		String s1;
		System.out.println("Please enter the name on the check (First Last) : ");
		s1 = kb.nextLine();
		System.out.println("Please enter the amount for the check (as a float) : ");
		float f1 = kb.nextFloat();
		if(f1 < 0)
			System.out.println("Invalid amount : " + f1);
		System.out.println("\n------------------------------------------------------");
		System.out.println("\nPay to : " + s1 + "\t\t\t\t$" + f1);
	}
}
Aepexx
Newbie Poster
12 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

... and? You now have some input to work with. What have you thought about doing with it?

How can you reduce that number to it's parts? You have to separate it before you can translate each to a string.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Are you trying to get at tokenizing it...? That's something that I had tried. I know how to tokenize from a String, but I don't know how to go about tokenizing a float to a string.

Aepexx
Newbie Poster
12 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Actually I'm referring to basic math to break it down to it's parts. Just think about the steps you mentally go through to write it out your self and then write the statements to gather each piece, like hundreds, tens, etc.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

I see what you're getting at. I can easily turn the 123.45 into one hundred twenty three...etc. What she wanted was for ANY sort of input to be turned into a string version of it. Not just specifically 123.45.

Sorry, I probably should have mentioned that earlier. But that's where I'm stuck at, turning any float into a written version of itself.

Aepexx
Newbie Poster
12 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

And that is what I was referring to.

How to you determine how many "hundreds" are in 123.45 (or other number)?
How many "tens"?
These are basic math operations.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Alright, thanks for your help. I'm just making it harder than it has to be. :P

Thanks again.

Aepexx
Newbie Poster
12 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: