so we've now got our calculator working
with the view model to separate the
logic from the user interface that's
given us a few advantages over the first
version we don't have to worry about
saving State for one thing the view
model persists across configuration
changes and doesn't lose its state when
the activity is recreated it subscribes
to the view model again and receives the
latest values to display but our
calculator works like a scientific
calculator it's not quite as useful for
financial calculations to see what I
mean run the app and let's work out tax
on a sale when the tax rate is 10% so
I'm going to start the emulator as fast
for this bit as always
all right so there's our calculator on
the screen so let's workout that tax to
work out the tax on $56 we multiply by
0.1 so let's try that in the calculator
I'm going to type 56 multiplied by 0.1
Eagles now for most scientific purposes
that's accurate enough but it's not very
good for financial calculations though
accountants don't really like it when
they see values like that but it can get
more obscure than that so if we enter
equals 56 equals to reset the value back
to 56 we'll do that first and we're
going to try it another calculation now
this time we're going to multiply by
0.01 so multiplied here Oh point zero
one equals and you can see we let the
value there of zero point five six
so that looks right now we should be
able to multiply that by a hundred to
get our original value back of a
multiplied by 100 equals so once again
we've got a strange rounding error there
the reason is that the decimal fraction
0.01 is a recurring value in binary now
I'm not going to go into the inner
details of storing decimal numbers you
can find out about I Triple E 754 to
learn more now there is actually a
Wikipedia article on I Triple E 75 for
that describes the standard quite well
I'll quickly bring this up on the screen
and you can check this out at your
leisure what we will do though is have a
look at how we can make our calculations
more suitable for financial work so
we're go back to Android studio and Java
has a big decimal class that solves
these strange rounding errors so what we
can do is create a version of our view
model that uses big decimal instead of
double and use that on our weapons did
so I'm going to go ahead and do that and
we'll start off by actually copying the
calculator view model class and pasting
it back into the cot the project so I'm
gonna copy and paste you can see that
IntelliJ has come up asked is what the
new name is I'm going to call this one
instead of calculator view model we'll
call this one big decimal view model
percent to that we've never got a big
decimal view model class I think that's
quite a descriptive name for what
ultimately it's going to be now there
aren't many changes to make it's quite a
simple change in fact we're just going
to be using the big decimal class
instead of the double class and if
you're offered a choice import from Java
math well so what we need to do is make
some changes firstly on line 15 we need
to change the type from double there to
a big decimal and you can see I'm
selecting there the java dot math import
i'll just take a copy of that because it
going to be pasting that in various
places we also need to make a change to
line 19 the result should be I'll type
big decimal now and no longer a type
double and we have got some errors
specifically we need to have a look in
the operand pressed function first see
at the moment we're using to double
their course that should be value safe
called dot to big decimal I'm gonna
change that to big decimal I've got an
error here which will fix shortly and
then scrolling down to the neck press
function we need to do a similar thing
here we've got to double we in use here
we need to change that to big decimal
I'm gonna make that change to DBT small
so we need to change this line here line
59 because we can't multiply a bit a big
decimal by double so that should be
double values star equals and it should
be big decimal dot value of then in
parenthesis we're going to put them on
as one so that's fixed up that fire
function now I'm moving down now to our
perform operation function will change
the declaration from value from a double
to a big decimal we need to change this
test here on line 79 currently if a
value equals equals is zero point zero
need to change that to big decimal dot
value of zero point zero in parentheses
which fixes the error then on the next
line need to handle the attempt to
divide by zero it shouldn't be WLAN it
should now be big decimal dot value of
so in parentheses double 13a
well that should be all the changes we
need to make here there's no longer any
errors in there so we now need to do is
go back to our main activity class we
need to change this line this view model
which is currently using the calculator
view model to use our bigdecimal view
model so we're going to take a copy of
that line and comment the first one out
and we're going to change this now to
use the incentive calculated view model
bigdecimal view model and that's
literally it
it should now use the bigdecimal view
model and be able to perform accurate
financial calculations let's try that
all right so we do the same resort we've
done before we'll start off by typing 56
multiplied by 0.1 equals this time we're
getting the answer 5.6 without all the
extra decimals so that's good the other
example we used was was first to see the
value back 256 we could do that with
equals 56 equals and then we multiplied
by 0.01 so multiplied by 0.01 equals to
0.5 6 so that looks right multiplying
that by a hundred we should get our
original value back so times 100 equals
this time it works we're getting fifty
six point two zero zero with no rounding
errors so switching back to main
activity we could have a menu option to
allow the user to choose between
scientific and financial modes all we'd
have to do is subscribe to the
appropriate view model in the code and
the calculators behavior would change
you may wonder why we don't just leave
it using big decimals and we certainly
could do that for this simple app
remember that we're learning about
general techniques here you may develop
this app further to include things like
sine and cosines these wouldn't be much
easier to an accountant and the
bigdecimal class doesn't support them
you can't find the cosine of a big
decimal in other words so with this
approach you can create a calculator
that offers the best of both worlds just
by switching the view model now before I
finish this really is a simple
and it may be overkill to use a view
model class in it
we did get some benefits though even
with an app as simple as this one in the
next video we'll start work on
converting the top 10 downloader app to
use a view model to manage its data see
you in the next video