1. 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. Member
Join Date
Jul 2011
Posts
39
Rep Power
0
I really want to diplay 0 at the end , give me some idea like before, how to do add 0 at the end.

3. 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. Member
Join Date
Jul 2011
Posts
39
Rep Power
0
Thanks for the reply, but what do you mean by pattern?

5. 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. Member
Join Date
Jul 2011
Posts
39
Rep Power
0
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. 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. Member
Join Date
Jul 2011
Posts
39
Rep Power
0
I know the pattern, when I enter 0 at the end after decimal then system omit 0 because of floating point.

9. 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. Originally Posted by gozuhair
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. Originally Posted by gozuhair
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 07:56 AM.

12. Member
Join Date
Jul 2011
Posts
39
Rep Power
0
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 09:37 AM.

13. Member
Join Date
Jul 2011
Posts
12
Rep Power
0
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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
@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. Member
Join Date
Jul 2011
Posts
39
Rep Power
0
Thanks for the solution but when i enter 12.90 then the output is 12 & 9.

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

double num1;
//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";

@others: CMIIW

17. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
@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. Member
Join Date
Jul 2011
Posts
1
Rep Power
0

## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
@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 First 12

#### Posting Permissions

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