Page 1 of 2 12 LastLast
Results 1 to 20 of 39
Like Tree1Likes

Thread: Separate Numbers

  1. #1
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default Separate Numbers

    Hi All

    I stuck with the following problem.
    Wanna break the numbers into two parts means before decimal points and after decimal points. for example:

    If I have a number 23.87 , how can i separate 23(before decimal point) and 87 (after decimal point).

    Regards

    Zuhair

  2. #2
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    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

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by fakepics500 View Post
    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
    That doesn't work for double type numbers > Integer.MAX_VALUE; better check the Math class for suitable methods or use the String version of the double type number (split around the decimal dot char '.')

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Alex.R is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    You could also do this:

    Java Code:
    double num = 23.87;
    int integral = (int) (num / 1);
    int remainder = (int)((num % 1) * 100);
    Alex

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  6. #6
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default

    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

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    What was wrong with the solutions you have been provided with so far?

  8. #8
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default

    I just checked it and its working great. Thanks

  9. #9
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default

    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.

    Java 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);
    	}
    }
    The output of the above program is
    Java Code:
     23 89
    .

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    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.

  11. #11
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default

    How can i perform string split, if i use alternative solution suggested.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    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

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.
    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().

  14. #14
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    444
    Rep Power
    6

    Default

    Although There are a lot of ways to do this. We better use NumberFormat class..
    Mak
    (Living @ Virtual World)

  15. #15
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default

    It means that i need to study NumberFormat class in order to get my desired output.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    No. If all you want to do is convert 12.34 to 12 & 34 then follow the steps I provided in reply #12.
    pbrockway2 likes this.

  17. #17
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default

    I know the first step and 3 step but dont know the second step How to split string.

  18. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    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.

  19. #19
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  20. #20
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default

    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]);
    
    		}
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  2. Using separate methods
    By Shyamz1 in forum New To Java
    Replies: 0
    Last Post: 10-31-2010, 05:31 PM
  3. How to separate return value ??
    By doha786 in forum New To Java
    Replies: 1
    Last Post: 03-10-2010, 03:43 PM
  4. Should I separate my code into separate files?
    By Inks in forum New To Java
    Replies: 0
    Last Post: 03-26-2009, 12:12 AM
  5. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •