Jump to content

Variable data wrong


Recommended Posts

I'm working on a script to return the total price of an item and everything works good up to the point where an item is deleted and the total price is updated. When the price hits "$0", the variable returns a random number. However, when another item is added the price is correct. The data is pulled from an excel file that I have created. The two portions of code below seems to be efficient enough to provide. If not, I can provide more. I'm not sure why this is doing what it's doing so I've come here for some much needed help. Thanks.

Case $msg = $but33

GUICtrlCreateListViewItem($var33 & "|$" & $price33,$LIST_VIEW)

$Total += $price33

GUICtrlSetData($Total_View,"|$" & $Total)

Case $msg = $oCancel

While 1

If Not GUICtrlRead($LIST_VIEW) Then ExitLoop

$Pget = _GUICtrlListView_GetItemTextArray($LIST_VIEW)

$Ptrim = StringTrimLeft($Pget[2],1)

$Total -= $Ptrim

GUICtrlSetData($Total_View,"|$" & $Total)

GUICtrlDelete(GUICtrlRead($LIST_VIEW))

WEnd

Link to comment
Share on other sites

Hello NickT,

it took me a while to figure out what you are doing here ... ;)

for debugging purposes, you can add some ConsoleWrite() statements to your code, for example like this:

$Pget = _GUICtrlListView_GetItemTextArray($LIST_VIEW)
ConsoleWrite("Reag from Listview: " & $Pet[0] & @TAB & $Pget[2] & @CRLF)
$Ptrim = StringTrimLeft($Pget[2],1)
Consolewrite("Value: " & $Ptrim & @CRLF)
Consolewrite("Old total: " & $Total& @CRLF)
$Total -= $Ptrim
Consolewrite("New total: " & $Total & @CRLF)
Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Thanks for the reply hannes08. This is what I got from the console:

Read from Listview: 2 $1.40

Value: 1.40

Old total: 1.4

New total: 1.77635683940025e-015

I'm not exactly sure why this is coming out the way it is. The "0" after the 4 in the "Old total" shouldn't have anything to do with it in my opinion. I could be wrong though.

Link to comment
Share on other sites

Hi Nick,

try converting your read value to a number first:

$Pget = _GUICtrlListView_GetItemTextArray($LIST_VIEW)
ConsoleWrite("Reag from Listview: " & $Pet[0] & @TAB & $Pget[2] & @CRLF)
$Ptrim = Number(StringTrimLeft($Pget[2],1))
Consolewrite("Value: " & $Ptrim & @CRLF)
Consolewrite("Old total: " & $Total& @CRLF)
$Total -= $Ptrim
Consolewrite("New total: " & $Total & @CRLF)

Maybe this will help... ;)

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Read from Listview: 2 $1.40

Value: 1.40

Old total: 1.4

New total: 1.77635683940025e-015

1.77635683940025e-015 = very close to zero 0.0000000000000017763563940025 and is a small rounding error due to computers being unable to store some decimal numbers precisely.

There are 2 ways you can avoid this problem:

1) Store and calculate all your prices in cents rather than dollars

2) Use the Round function

While 1
   If Not GUICtrlRead($LIST_VIEW) Then ExitLoop
   $Pget = _GUICtrlListView_GetItemTextArray($LIST_VIEW)
   $Ptrim = StringTrimLeft($Pget[2],1)
   $Total -= $Ptrim
   $Total = round($Total,2)
   GUICtrlSetData($Total_View,"|$" & $Total)
   GUICtrlDelete(GUICtrlRead($LIST_VIEW))
WEnd
Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

hannes08, unfortunately converting to a number did not work. I still get the same results. Another thing that has me confused is that when I click on another button to add the price to the list, the total is correct. It matches the price of the item that was clicked on.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...