# Separate Numbers

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-19-2011, 02:23 PM
gozuhair
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
• 07-19-2011, 02:56 PM
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
• 07-19-2011, 03:48 PM
JosAH
Quote:

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
• 07-19-2011, 04:29 PM
Alex.R
You could also do this:

Code:

```double num = 23.87; int integral = (int) (num / 1); int remainder = (int)((num % 1) * 100);```
Alex
• 07-19-2011, 04:32 PM
Dark
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.
• 07-20-2011, 05:52 AM
gozuhair
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
Junky
What was wrong with the solutions you have been provided with so far?
• 07-20-2011, 05:57 AM
gozuhair
I just checked it and its working great. Thanks
• 07-20-2011, 06:05 AM
gozuhair
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.

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
Code:

` 23 89`
.
• 07-20-2011, 06:08 AM
Junky
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
gozuhair
How can i perform string split, if i use alternative solution suggested.
• 07-20-2011, 06:34 AM
Junky
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
pbrockway2
Quote:

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().
• 07-20-2011, 07:11 AM
makpandian
Although There are a lot of ways to do this. We better use NumberFormat class..
• 07-20-2011, 07:16 AM
gozuhair
It means that i need to study NumberFormat class in order to get my desired output.
• 07-20-2011, 07:18 AM
Junky
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
gozuhair
I know the first step and 3 step but dont know the second step How to split string.
• 07-20-2011, 07:34 AM
Junky
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
pbrockway2
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
gozuhair
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:

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]);                 } }```
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last