manu
manu

Reputation: 77

Obtain Decimal values after Division

How can i obtain the decimal value after division ? after doing this

days = miliseconds/(24 * 60 * 60 * 1000)

suppose the value(i.e days) is 27.8351 how do i get 8351 ? Tried

hours = days % (int) days thought it s doing 27.8351 / 27 but still it returned a big Zero, havent tried converting it into string and then splitting based on DOT (.) but i dont like this way have i over looked some thing ?

Upvotes: 0

Views: 1739

Answers (3)

kgiannakakis
kgiannakakis

Reputation: 104168

You should use BigDecimal for this kind of operations. The divideAndRemainder method is available.

Upvotes: 1

Abhinav Sarkar
Abhinav Sarkar

Reputation: 23792

hours = (days - (int) days) * 10000

(to get 4 digits after decimal)

Upvotes: 0

Neigyl R. Noval
Neigyl R. Noval

Reputation: 6038

There are many ways to do this.

If you know in advance the number of decimal places, then subtract the result by the whole number and multiply it 10 to power of the number of decimal places.

Example: # of dec places is 4 and the result is 42.8798

Process:

(1) 42.8798 - 42 = 0.8798

(2) 0.8798 * (10^4) = 8978

Upvotes: 0

Related Questions