here's a slower but sometimes more precise way, e.g., if you're
dealing with currency:
double d = 124.50d;
BigDecimal ii = new BigDecimal(""+d);
Log.d("whole and decimal","BigDecimal ii = "+ii+" = new BigDecimal
(\"\"+d); d = "+d);
Log.d("whole and decimal","whole = ii.intValue() = "+ii.intValue
());
Log.d("whole and decimal","decimal = ii.remainder(BigDecimal.ONE)
= "+ii.remainder(BigDecimal.ONE));
extracts from the log:
BigDecimal ii = 124.5 = new BigDecimal(""+d); d = 124.5
whole = ii.intValue() = 124
decimal = ii.remainder(BigDecimal.ONE) = 0.5
sm1
On Mar 23, 3:28 pm, droozen <[email protected]> wrote:
> I'm a bit confused about your question. You ask a question and then
> pretty much answer it. :) There's other ways, as you say, but that's
> pretty much how you do it in java, too.
>
> x = 124.5
> y = x - int(x) (so now y = 0.5 and x = 124.5)
> z = x - y (so z = 124.0
>
> a = y * 3 ( a now = 1.5 )
> b = z + a ( here's you're only error. 124 + 1.5 will give you b =
> 125.5, unless you are casting it to an int)
>
> Some other ways.
>
> String myX = Double.toString(x); (gives you x as a string)
> String s = myX.substring(myX.indexOf(".") + 1) (s = everything after
> the .)
> String r = myX.substring(0, myX.indexOf(".") (r = everything before
> the .)
>
> a = Double.parseDouble(s) * 3 (Turn everything after the . back into a
> double and multiply)
> b = Double.parseDouble(r) + a (And turn everything before the . back
> into a number and finish your calc
>
> Of course, using string manipulation like this you'll have to have a
> lot more checks for nulls and whatnot (what if there is no decimal
> part, converting it to a string might not yield a . in the string).
>
> Best way is as you posted, though.
>
> On Mar 20, 10:36 pm, Jintsubo <[email protected]> wrote:
>
> > So I having a good crack at my first Android app, I've completed all
> > the tutorials and suddenly realised I haven't a clue about how to do
> > simple mathematics in Java. I have some (self taught) experience in
> > PHP, Python etc, I'm no expert but I have managed to get the things
> > done that I want to do.
>
> > What I can do in Python but can't for the life of me figure out in
> > javndroid; how to separate the decimal from the whole.
>
> > for example, 124.50. I want to do some calcs using only the 0.50 and
> > then when I'm finished, add it back onto the 124.0.
>
> > x = 124.50
> > y = x - int(x) (there are other ways but for simplicity sake)... y =
> > 0.50
> > z = x - y = 124.0
>
> > a = y * 3 = 1.5
> > b = z + a = 125.0
>
> > Any pointers greatly appreciated.
>
> > JR
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---