views:

98

answers:

3

Hi,

I'm looking to do the following : let's say I have a number like 125.625 => I would like to be able to tell my program that is 1 * 100 + 2 * 10 + 5 * 1 + 0.625. How could I do that please ?

thanks

+1  A: 

If you have your number as an integer, you could do something like this:

value := 125.625
fractional part := value % 1
value := floor(value)

exponent = 1
listofvaluepairs = array()
while(value > 0)
{
  tmp_part := value % 10
  listofvaluepairs.add(value, exponent)
  exponent = exponent * 10
  value = floor(value / 10)
}

The above psuedocode will fill the listofvaluepairs with pairs of the digit and the number to multiply by. So you'd get this:

{{1,100}, {2,10}, {5,1}}

And the fractional part has that last piece you wanted.

For reference, the floor() function will give the number without the fractional part, so floor(1.6) gives 1. The % (modulo) operator will more or less give the remainder after division. So 7%2 = 1.

What I do in the above example (which is very much not java code), is get one digit at a time from the value.

So I start with 125:

125 % 10 = 5
Then:
floor(125 / 10) = floor(12.5) = 12

Then I repeat to get 2 and 1. At the same time I'm saving 1, 10, and 100 using the exponent variable.

JoshD
wow that was brilliant. Could you please explain the maths behind this. because i was going to do it the long way. which was to convert the number into a string then split the number up and start to then do some next long things with the string. which i knew would have been the wrong way to solve it anyway. but how does your maths work? im not familiar with words like exponent and floor value and so on. could you please tell me how your code works? thank you.. reason why is because i want to understand the logic behind it so i can apply it elsewhere rather than just copying and pasting it.
Pavan
@JoshD: You completely missed the decimal part, pal.
Adeel Ansari
@Adeel Ansari: it's in `fractional part`. It's the second line. (Keep in mind this is psuedocode.)
JoshD
First, he grabbed the last digit, which would be x * 1, by doing `value % 10`. Then advanced to the next exponent (x * 10, and so on) with `exponent = exponent * 10`, and shifted the number one digit to the right (discarding the decimal part) with `floor(value / 10)`. Rinse and repeat for each digit. Once all digits are right-shifted-and-floor'd to oblivion, the result is 0 and the loop exists.Fractional part is simply `originalValue % 1`. (You can even fold that into the while and get fractional digits)
Kyte
Exponent = The y in `x to the y-th power`. Floor = Round down. Its opposite is Ceiling, which rounds up.
Kyte
@JoshD: Sorry, but by the word integer, as you stated in the very first line of your post, my mind chopped off all the fractional parts already. :)
Adeel Ansari
A: 

You can get each digit by using the modulo or % operator in a loop for the integer values (i.e.: int ones = myVal % 10), and to get the fractional value do float fraction = val - (int)val;

Bryan
+3  A: 
double num = 125.625;
System.out.print(num - (int)num);
num = (int) num;

for (int i=10; num!=0; i*=10) {
    System.out.print(" + " + i/10 + " * " + (num % i)/i*10);
    num -= num % i;
}

Will print:

0.625 + 1 * 5.0 + 10 * 2.0 + 100 * 1.0

Of course you can change the code to add stuff to a data structure.

NullUserException
+1. By the way, you folks should refrain from doing their assignments, especially when you don't see any effort on their part.
Adeel Ansari
IF someone wants to answer the guy's homework that is fine. The OP can easily learn more from seeing someone's correct answer than from writing up the hackjobs you do in school. It all depends on what he wants to get out of it.
Andrew Hubbs
@Andrew Hubbs: You can't just learn by watching someone swimming. The analogy fits here too, IMO. So, they must try and come out with some code, no matter if hackjobs. At least put your brain to work and try solve the problem. Its good to understand by reading someone else's code. But writing own code is different experience altogether. And I believe those hackjobs really yield you some valuable skills.
Adeel Ansari
Yeah, you'll never win gold without jumping in the pool and sometimes a little nudge helps. No argument there, but stackoverflow is a Q/A for professionals, not a college ask your TA session. Baiting people with partial answers (even if it may or may not be for their own good) isn't a part of that. The reason I ask/answer questions here is that I know I can expect high quality complete answers from experienced developers. I think that's important.
Andrew Hubbs
I suggest using long rather than int.
Peter Lawrey
Andreas_D
@Andreas_D: Yes, thats the point, profiting from good solutions. But first show me the one you got, any efforts made?
Adeel Ansari
@Adeel - my efforts? Hey, that's not **my** assignment, I'm just commenting ;)
Andreas_D
@Andreas_D: Of course not yours, but the poster in question.
Adeel Ansari