Page 2 of 2 FirstFirst 12
Results 21 to 39 of 39
Like Tree1Likes

Thread: Separate Numbers

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

    Default

    That is because the 0 after the 9 in a floating point number does not exist. 23.90, 23.900, 23.9000, 23.900000000000000000000000000000000 etc are all equivalent to 23.9. The number of zeros added at the end are insignificant. To do what you want then you will have to add some extra code to determine if you should display a 0 at the end or not.

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

    Default

    I really want to diplay 0 at the end , give me some idea like before, how to do add 0 at the end.

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

    Default

    If you use the String.split method then:
    12.34 gives 12 & 34
    43.76 gives 43 & 76
    98.257 gives 98 & 257
    78.09 gives 78 & 09
    37.04 gives 37 & 04
    12.30 gives 12 & 3
    45.80 gives 45 & 8

    Can you see a pattern?

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

    Default

    Thanks for the reply, but what do you mean by pattern?

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

    Default

    Look at the original value on the left and the split value on the right. When the original value is .... you get .... and when the original value is .... you get ....

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

    Default

    All the output is fine except last one, how can I write such type of code in whick I enter 45.80 and my result will be 45 and 80 not 45 & 8, you told me that I need to add some extra code, could you please give me some idea how to write. Thanks

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

    Default

    FFS

    Are you yanking my chain? Can you not see that the value 12.37 will yield 37 and that value 12.9 will yield 9 and that you cannot see the difference between 37 and 9 and do not know of any way to test the difference between 37 and 9. This is not a programming issue. It is a simple logic issue. Once you know what the logic is, writing the code is simple.

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

    Default

    I know the pattern, when I enter 0 at the end after decimal then system omit 0 because of floating point.

  9. #29
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Check the result after executing of the following line of code.

    Java Code:
    String str = Double.toString(num);
    Just add a print statement after this for the variable str Looking at the result think why it is really happen. You must read the Java API.

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

    Default

    Quote Originally Posted by gozuhair View Post
    I know the pattern, when I enter 0 at the end after decimal then system omit 0 because of floating point.
    That is not a pattern. What I'm talking about is that 12.37 yields 37, a 2 char String. While 12.90 yields 9 a single char String.

    Your lack of ability to think for yourself is making kittens cry.

  11. #31
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by gozuhair View Post
    Thanks for the reply, but what do you mean by pattern?
    i'll try to explain (not to be a smart*ss :)) ) what junky mean is: if the input is e.g: 23.90 then what store in your variable, which is a double type, is 23.9 compare this to the input of: 57.89. it store exactly: 57.89 in your variable. so you can see the pattern that if you split it into strings then it should be: "23" and "9", and "57" and "89" respectively. you can see that what junky mean about "pattern" in here is there's a only a single character in you string variable. so, the solutions can be carious, one of them is the most simplest way is: you can check whether the string length is 1 or 2.

    if the length is 1 you can add a 0 (zero) in it.

    maybe you're not very familiar with this kinda situation, like don't have an idea about what's junky said, because your algorithm basic isn't good enough. because, actually, if you wanna learn programming the first thing you should learn is algorithm. so i'm suggesting you to find (many) good reading about algorithm first. it will do you a lot of help... afterall, keep the hard workin' dude...!

    btw, CMIIW
    Last edited by chipp; 07-21-2011 at 08:56 AM.

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

    Default

    Thanks for the friendly explanation, but suppose if I use the below logic
    Java Code:
    if the length is 1 you can add a 0 (zero) in it.
    and I enter 23.9(means only 1 digit after decimal) not 23.90 then system automatically add 0 into it and the output is become 23 & 90 not 23 & 9. Please suggest.
    Please also suggest me a good and beginner level algorithm book, if it is useful for me to get into java programming.
    Last edited by gozuhair; 07-21-2011 at 10:37 AM.

  13. #33
    amanrv is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    convert it to string first, that makes the thing easy.

    Java Code:
    double n=80.12;
    String convert=Double.toString(n);
    int l=convert.length(); 
    String s1="";
    String s2="";
    for(int i=0;i<l;i++)
    {
    int a=convert.indexOf('.');
    if(i<a)
    {s1=s1+convert.charAt(i);
    }
    else if(i==a)
    {
    }
    else
    {s2=s2+convert.charAt(i);
    }
    }
    int s11=Integer.parseInt(s1);
    int s22=Integer.parseInt(s2);
    //the code after here is just for displaying the numbers. its optional
    System.out.println(s11);
    System.out.println(s22);

  14. #34
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    @amanrv: wouldn't it be easier to use split() as already discussed in this thread, rather than the for loop? Also, did you check your code against the 12.90 case mentioned above?

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

    Default

    Thanks for the solution but when i enter 12.90 then the output is 12 & 9.

  16. #36
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default

    what i'm talkin' about is the "back stage" operational. like:

    double num1;
    system.println("enter your number");
    //enter to num1 (i didn't know input stream in java)
    string separator = double.tostring (num1);
    //separate the first integer (after comma) and the second (behind the comma)
    //assuming there's two variables to store the two values

    string var1, var2;

    //var2 is for the second value (behind comma, right side)
    //add the 0 after check the length is it 1 or 2 digits
    //if the length of var2 is 1 then

    var2 += "0";

    about algorithm, you can search it in google, or askin' the other about good reading

    @others: CMIIW

  17. #37
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    @chipp: had to google that! (it's a buggar getting old ... but enough brain is still functioning to be able to have guessed it)

    system.println() should be System.out.println()
    double.tostring(num1) should be ""+num1
    string should be String
    // I think the OP's separator is a dot not a comma

    @OP: The kernel of chipp's post is worth thinking about

    //if the length of var2 is 1 then
    var2 += "0";

    There is a String method to get the length of var2.

    Try it: write some code and post it if you get stuck.

  18. #38
    smartshiva is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default Can try this

    You can try the below one but I doubt how efficient is this
    Java Code:
                             double amnt = 23.43;
    		String str1 = amnt + "";
    		String[] str2 = str1.split("\\.");
    		System.out.print("You owe me "+str2[0]+" Dollars and "+str2[1]+ " Cents.");
    and for conversion,
    Java Code:
    		double temp1 = Double.parseDouble(str2[0]);
    Regards
    Shiva

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

    Default

    @smartshiva: splitting on dot is not enough. You're 81 cents short of the full $12.90 as has been mentioned quite a few times in this thread.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 11
    Last Post: 01-14-2011, 07:36 PM
  2. Using separate methods
    By Shyamz1 in forum New To Java
    Replies: 0
    Last Post: 10-31-2010, 06:31 PM
  3. How to separate return value ??
    By doha786 in forum New To Java
    Replies: 1
    Last Post: 03-10-2010, 04: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, 01:12 AM
  5. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM

Posting Permissions

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