1. Member
Join Date
Jul 2011
Posts
39
Rep Power
0

## 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. 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

3. Originally Posted by fakepics500
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

4. Member
Join Date
Jul 2011
Posts
3
Rep Power
0
You could also do this:

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

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.

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

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

8. Member
Join Date
Jul 2011
Posts
39
Rep Power
0
I just checked it and its working great. Thanks

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.

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. 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. Member
Join Date
Jul 2011
Posts
39
Rep Power
0
How can i perform string split, if i use alternative solution suggested.

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

13. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
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. Although There are a lot of ways to do this. We better use NumberFormat class..

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.

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

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.

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.

19. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
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. 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]);

}
}```

Page 1 of 2 12 Last

#### Posting Permissions

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