programming help (1 Viewer)

phizz

aka: Philly Cheese
Joined
Jul 4, 2004
Messages
131
Location
Wauchope (port mac')
Gender
Male
HSC
2004
Just a bit of a mind blank at the moment, firstly I'm programming in Java, but any algorithm solution will help.

Basically I want to store the decimal value from a real varible, eg:

526.78

I want to be able to store 0.78 sepperatly.

so far its programmed like:

double num; (num is then inputted by user)

double decimal; (place to store decimal value of num)

any help will be very much appreciated - phizz
 
Last edited:

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
1) Floor the input number
2) Subtract it from the original number
3) You're left with the decimal
 

jm1234567890

Premium Member
Joined
Aug 18, 2002
Messages
6,516
Location
Stanford, CA
Gender
Male
HSC
2003
or

cast into string
delete bits before decimal point
cast back to double

lol, gotta love how simplistic it is to play around with strings in java.
 

jonathan109

Member
Joined
Nov 12, 2004
Messages
33
Gender
Undisclosed
HSC
N/A
Cast the double to a String.

Invoke a indexof method call passing in ".".

Sub string it and cast it back into the appropriate format.

Since you're splitting it up, they're both going to be integer's so store them both back into integers.
 

poloktim

\(^o^)/
Joined
Jun 15, 2003
Messages
1,323
Location
Wollongong
Gender
Male
HSC
2003
jonathan109 said:
Cast the double to a String.

Invoke a indexof method call passing in ".".

Sub string it and cast it back into the appropriate format.

Since you're splitting it up, they're both going to be integer's so store them both back into integers.
Agreed here. If memory serves me correctly, doubles are the size of two words (sixty-four bits), so storing two doubles means you've got one hundred and twenty-eight bits. Storing as two integers (thirty-two bits) means you've only used up half that (sixty-four bits).

Of course that's just for thrity-two bit systems (though Java may be standard even for the better sixty-four bit machines). Anyone with a G5/AMD64/Intel equivalent able to check?
 

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
poloktim said:
Of course that's just for thrity-two bit systems (though Java may be standard even for the better sixty-four bit machines). Anyone with a G5/AMD64/Intel equivalent able to check?
Bit width is fixed by the JVM.
 

poloktim

\(^o^)/
Joined
Jun 15, 2003
Messages
1,323
Location
Wollongong
Gender
Male
HSC
2003
jm1234567890 said:
no, VM is standard, which is why code is portable
Thanks. :)

That's a bit of a kick up the bum for sixty-four bit machines though. The VM isn't utilising the computer's full potential. :(
 

jm1234567890

Premium Member
Joined
Aug 18, 2002
Messages
6,516
Location
Stanford, CA
Gender
Male
HSC
2003
poloktim said:
Thanks. :)

That's a bit of a kick up the bum for sixty-four bit machines though. The VM isn't utilising the computer's full potential. :(
nah, if the vm is programmed well, it could
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top