chun914 Posted May 31, 2013 Share Posted May 31, 2013 I have some numbers like below and the final $temp amount should be 450 exact but the calculation ended up with 450.00000000001.. i have no idea what's going wrong ? $p = 4592.05 $1 = 679.41 $2 = 661.31 $3 = 660.04 $4 = 648.06 $5 = 963.85 $6 = 529.38 $childsum = 0 $childsum += $1 msgbox(0,"",$childsum) $childsum += $2 msgbox(0,"",$childsum) $childsum += $3 msgbox(0,"",$childsum) $childsum += $4 msgbox(0,"",$childsum) $childsum += $5 msgbox(0,"",$childsum) $childsum += $6 msgbox(0,"",$childsum) $temp = $p -$childsum msgbox(0,"",$temp) Link to comment Share on other sites More sharing options...
AdamUL Posted May 31, 2013 Share Posted May 31, 2013 This is due to floating point arithmetic. Since none of your numbers has a precision greater than two decimal places, use the following. Round($temp, 2) Link to comment Share on other sites More sharing options...
chun914 Posted May 31, 2013 Author Share Posted May 31, 2013 (edited) i have to check if the sum + 450 = $p....... if $childsum + 450 = $p Then msgbox(0,"","so something") Else msgbox(0,"","oops") EndIf Edited May 31, 2013 by chun914 Link to comment Share on other sites More sharing options...
AdamUL Posted May 31, 2013 Share Posted May 31, 2013 (edited) Then use the following after your arithmetic. $childsum = Round($childsum, 2) Edited May 31, 2013 by AdamUL Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 31, 2013 Moderators Share Posted May 31, 2013 chun914,As all the amounts have 2 decimal places I would convert all of them to integers (i.e. multiply them by 100) so that I could use integer arithmetic to sum them. Then I would divide by 100 and use Round when I wanted the total back in 2 decimal place format. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
chun914 Posted June 3, 2013 Author Share Posted June 3, 2013 thanks.. M23, finally get it done by *100 and compare using int($a) = int($ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now