Why {29.19 + 0.05} = 29.24000000002

Post your questions and help other users.

Moderator: Martin

Post Reply
natong
Posts: 80
Joined: 29 Aug 2015 05:24

Why {29.19 + 0.05} = 29.24000000002

Post by natong » 07 Jul 2018 17:28

Screenshot_2018-07-08-00-25-19-030.jpeg
Screenshot_2018-07-08-00-25-19-030.jpeg (264.8 KiB) Viewed 11061 times
Screenshot_2018-07-08-00-28-51-789.jpeg
Screenshot_2018-07-08-00-28-51-789.jpeg (294.72 KiB) Viewed 11061 times
Please suggestion.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Why {29.19 + 0.05} = 29.24000000002

Post by Desmanto » 08 Jul 2018 17:56

floating calculation problem. At times, it will throw such problem. You have to round it. Unfortunately we don't have round() with extra argument to limit the decimal yet. You can use either one of these solution

Code: Select all

a = 29.19 + 0.05 ; // result = 29.24000000002
b = round(a*100)/100; // result 29.24
c = "{a,numberformat,0.00}";  // result 29.24
The b is using universal rounding method, it can round to any number (to round to nearest 7, replace the 100 with 7).
While c is using automagic built-in pattern character to show the number in 2 decimals.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

natong
Posts: 80
Joined: 29 Aug 2015 05:24

Re: Why {29.19 + 0.05} = 29.24000000002

Post by natong » 09 Jul 2018 10:36

Thank. These bug will be fixed in the next version ? or it's android floating bug and can't be fixed ?

User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Re: Why {29.19 + 0.05} = 29.24000000002

Post by digitalstone » 09 Jul 2018 12:29

Not even Android i think. More a fundamental problem/side effect.
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Why {29.19 + 0.05} = 29.24000000002

Post by Desmanto » 09 Jul 2018 17:41

Yeah, I think it is featured, as Automagic scripting is inherited from java/javascript. Besides, floating calculation problem is a common problem. We have to deal with it, just use round or other method to show it properly. I use both b and c method depends on my need.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply