Results 1 to 20 of 39
Thread: Separate Numbers
- 07-19-2011, 02:23 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 39
- Rep Power
- 0
- 07-19-2011, 02:56 PM #2
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
i think you can do this
let orginal no be no
take the number store it in a int type (ino)
ino=no;
no=no-ino;
no=no*100;
eg:
my number is 23.87
ino=23
no=23.87-23
no=.87
no*100=>87
- 07-19-2011, 03:48 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-19-2011, 04:29 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
You could also do this:
AlexJava Code:double num = 23.87; int integral = (int) (num / 1); int remainder = (int)((num % 1) * 100);
- 07-19-2011, 04:32 PM #5
I would just turn it into a string and then do a string split. What are you trying to do with this? What is the program supposed to do, because there might be a better way if you plan on manipulating the numbers further.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-20-2011, 05:52 AM #6
Member
- Join Date
- Jul 2011
- Posts
- 39
- Rep Power
- 0
Hi
First of all, thanks for your replies, I am doing one of my assignment and stuck with the above query, actually program requirement is that if i enter some double datatype like 23.87 then system automatically display the following message.
23 dollars and 87 cents.
Thanks
- 07-20-2011, 05:55 AM #7
What was wrong with the solutions you have been provided with so far?
- 07-20-2011, 05:57 AM #8
Member
- Join Date
- Jul 2011
- Posts
- 39
- Rep Power
- 0
I just checked it and its working great. Thanks
- 07-20-2011, 06:05 AM #9
Member
- Join Date
- Jul 2011
- Posts
- 39
- Rep Power
- 0
oooh sorry, its working fine with 23.87 but if i change the value from 23.87 to 23. 90 by using the follwing code then it gives the wrong result.
The output of the above program isJava Code:public class separatestrings { public static void main(String[] args) { double num = 23.90; int integral = (int) (num / 1); int remainder = (int)((num % 1) * 100); System.out.println(integral + " " + remainder); } }.Java Code:23 89
- 07-20-2011, 06:08 AM #10
That would be due to the known problems associated with floating point numbers (google "What every computer scientist should know about floating point numbers"). Try the alternative solution suggested.
- 07-20-2011, 06:27 AM #11
Member
- Join Date
- Jul 2011
- Posts
- 39
- Rep Power
- 0
How can i perform string split, if i use alternative solution suggested.
- 07-20-2011, 06:34 AM #12
Convert the floating point number into a String (method in String or Double class)
Call split on that String
Parse Strings back into an int
If you do not know how to do any of these steps then consult the Java API. Then try writing some code and see what happens. If you get stuck then come back and ask a specific question. "It doesn't work" is not specific nor a question
- 07-20-2011, 07:05 AM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
That sounds like a formatting requirement. Java has a DecimalFormat class that you might find useful, String has a format() method and other classes have printf(). These make more sense than mucking about with arithmetic operations if you wish to format: for instance that message would be a one liner using System.out.printf().actually program requirement is that if i enter some double datatype like 23.87 then system automatically display the following message.
23 dollars and 87 cents.
- 07-20-2011, 07:11 AM #14
Although There are a lot of ways to do this. We better use NumberFormat class..
Mak
(Living @ Virtual World)
- 07-20-2011, 07:16 AM #15
Member
- Join Date
- Jul 2011
- Posts
- 39
- Rep Power
- 0
It means that i need to study NumberFormat class in order to get my desired output.
- 07-20-2011, 07:18 AM #16
No. If all you want to do is convert 12.34 to 12 & 34 then follow the steps I provided in reply #12.
- 07-20-2011, 07:30 AM #17
Member
- Join Date
- Jul 2011
- Posts
- 39
- Rep Power
- 0
I know the first step and 3 step but dont know the second step How to split string.
- 07-20-2011, 07:34 AM #18
You would use the split method in the String class. Go to the Java API and read about it, learn and then make an attempt at writing the code.
- 07-20-2011, 07:39 AM #19
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Ignore my reply #13 - the formatting methods are good but not directly applicable here as you still have to parse out the two pieces. Follow Junky's advice, but be careful that it understands 23.90 has 90 cents and not 9.
- 07-20-2011, 09:03 AM #20
Member
- Join Date
- Jul 2011
- Posts
- 39
- Rep Power
- 0
I used the following code in order to convert double to string and then split string, but i am facing two problems. one problem is that when i try to give value like 23.99 then its split well but when i assign value 23.90 then it gives 23.9 and does not include 0, my other problem is that I split my string by using array and I dont know what to do next because I am unable to parse string array to double. my codes are below:
Java Code:public class separatestrings { public static void main(String[] args) { double num = 23.90 ; String str = Double.toString(num); String[] temp; //delimeters String delimeters = "\\."; /* given string will be split by the argument delimiter provided. */ temp = str.split(delimeters); //print substring for(int i =0; i < temp.length ; i++) System.out.println(temp[i]); } }
Similar Threads
-
Problem getting numbers from user and finding smallest two numbers
By radhi16 in forum New To JavaReplies: 11Last Post: 01-14-2011, 06:36 PM -
Using separate methods
By Shyamz1 in forum New To JavaReplies: 0Last Post: 10-31-2010, 05:31 PM -
How to separate return value ??
By doha786 in forum New To JavaReplies: 1Last Post: 03-10-2010, 03:43 PM -
Should I separate my code into separate files?
By Inks in forum New To JavaReplies: 0Last Post: 03-26-2009, 12:12 AM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks