Jump to content

Working out denominations of a figure


Phaser
 Share

Recommended Posts

Hi everyone

Could someone show me the correct way to do this in Autoit please

Local $order,$bags_needed[6]
Local $bag_values[7] = [25,10,5,1,0.50,0.10,0.01]

$order = 799.16 ;-- current order   

For $i = 0 to UBound($bag_values)                                 
    $bags_needed = floor($order/$bag_values[$i])     
    $order = $bag_values[$i]*$bags_needed[] ;<----- error here                         
Next

_ArrayDisplay($bags_needed)

I would like it to produce the following for the order of 799.16 grams

25 bag x 31

10 bag x 2

1 bag x 4

0.50 bag x 0

0.10 bag x 1

0.01 bag x 6

Hope its explained good enough, is there an easier way for the above?

Link to comment
Share on other sites

  • Moderators

Phaser,

I would do it this way: :x

#include <Array.au3>

Global $aBag_Values[7] = [25, 10, 5, 1, 0.50, 0.10, 0.01]
Global $aBag_Number[7]

Global $nOrder = 799.16 ;-- current order

; Make into an integer to avoid rounding errors ;)
$nOrder = $nOrder * 100

For $i = 0 To UBound($aBag_Values) - 1 ; Note you need 1 less that the total number because you have element [0] ;)

    ; How many of this size bag can we get?
    $aBag_Number[$i] = Int($nOrder / ($aBag_Values[$i] * 100))
    ; Reduce the value by that many bags 
    $nOrder -= $aBag_Values[$i] * $aBag_Number[$i] * 100

Next

_ArrayDisplay($aBag_Number)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

mm just noticed a problem, sometimes the order can be sent through for 799 x 0.10 grams, I would then like to calculate the 799 0.10 units to produce 79.9 which would give me

25 x 3

10 x 0

5 x 0

1 x 4

0.5 x 1

0.10 x 4

0.01 x 0

And if it was 799 x 0.01 grams it would be 7.99

25 x 0

10 x 0

5 x 1

1 x 2

0.50 x 1

0.10 x 4

0.01 x 9

Hope it makes sense, thanks again

Link to comment
Share on other sites

  • Moderators

Phaser,

Do you know any basic maths? :x

Good job I am not starting to redecorate the kitchen until tomorrow! :P

#include <Array.au3>

Global $aBag_Values[7] = [25, 10, 5, 1, 0.50, 0.10, 0.01]
Global $aBag_Number[7], $aLabels[7]

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

GUICtrlCreateLabel("Chose Unit", 10, 10, 100, 20)
$hCombo = GUICtrlCreateCombo("", 10, 30, 100, 20)
GUICtrlSetData($hCombo, "25|10|5|1|0.50|0.10|0.01")

GUICtrlCreateLabel("Input Number", 150, 10, 100, 20)
$hInput = GUICtrlCreateInput("", 150, 30, 100, 20)

$hButton = GUICtrlCreateButton("Calculate", 10, 100, 60, 30)

For $i = 0 To 6
    $aLabels[$i] = GUICtrlCreateLabel($aBag_Values[$i] & ":", 10, 150 + (20 * $i), 200, 20)
Next

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton
            _Calculate()
    EndSwitch

WEnd

Func _Calculate()

    $nAmount = GUICtrlRead($hInput) * GUICtrlRead($hCombo) * 100

    For $i = 0 To UBound($aBag_Values) - 1
        $aBag_Number[$i] = Int($nAmount / ($aBag_Values[$i] * 100))
        $nAmount -= $aBag_Values[$i] * $aBag_Number[$i] * 100
    Next

    For $i = 0 To 6
        GUICtrlSetData($aLabels[$i], $aBag_Values[$i] & ":" & @TAB & $aBag_Number[$i])
    Next

EndFunc

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Do you know any basic maths?

I do actually but only as basic as making sure I get the correct change!!

Sorry for my weakness but thanks you very much, I hope your kitchen refurb goes well and Mrs Melba is happy with the work.

Thanks again, much appreciated

Link to comment
Share on other sites

Hi Melba23

I noticed a few incorrect results last night and, with my very limited basic maths, cant understand why it's not working properly.

Try putting 0.01 unit size and 31 as the input number, it loses the 1 also 41 loses the 1

Try 25 unit and enter 100 in the input it should return 25 x4 it says 25 x 100

I hope you can figure it out while on coffee break from your kitchen redecorating.

Many thanks if you find the time.

Link to comment
Share on other sites

  • Moderators

Phaser,

The redecorating is going fine, thanks. :x

The missing 1 is a rounding problem - look up floating point arithmetic if you want to know more about it. I thought I had removed that problem with the "* 100" trick, but it raised its Hydra-like head again. Just change this line:

$nAmount -= Round($aBag_Values[$i] * $aBag_Number[$i] * 100)

and you should have it beaten. :P

The second problem is not a problem. You asked for the amount to be the product of the unit and the amount. So why are you surprised when you are told you need 100 x 25-units to fill an order for (100 * 25) = 2500? If you want only 4 x 25-units, then you need to order 100 x 1-units or 4 x 25-units. All clear? :shifty:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...