Expression := or Round() not working as expected Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
RickC
Posts: 302
Joined: 27 Oct 2013, 08:32

Expression := or Round() not working as expected

22 May 2024, 05:50

Code: Select all

#SingleInstance, Force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; Function to get total physical memory using Get-CimInstance
GetTotalPhysicalMemory()
{
    ; Define the PowerShell command
    psCommand := "powershell -command ""& { Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -ExpandProperty TotalPhysicalMemory }"""

    ; Run the PowerShell command and copy the result to the clipboard
    RunWait, %ComSpec% /c %psCommand% | clip, , Hide

    ; Retrieve the result from the clipboard
    ClipWait, 2
    totalMemory := Clipboard

    ; Convert the result from bytes to megabytes
    totalMemoryMB := Round(totalMemory / 1MB, 2)
    
    MsgBox, %totalMemory% ; This shows 8464130048 (bytes) correctly
    MsgBox, %totalMemoryMB% ; This shows an incorrect result of 0.00... I was expecting 8.00 

    return totalMemoryMB
}

; Example usage of the function
totalMemoryMB := GetTotalPhysicalMemory()
MsgBox, Total Physical Memory: %totalMemoryMB% MB

GuiClose:
ExitApp
I have a laptop with 8MB RAM, running AutoHotkey 1.1 (1.1.37.01) in Windows 10 (1809).

In the code above I can use PowerShell 5.1 to query the amount of RAM and send the result to the Windows clipboard. Both CTRL+V and MsgBox show the amount of RAM in bytes correctly.

Next I try to display the amount in MB, rounded to 2 decimal places. I'm expecting to see 8.00 MB.. but it shows 0.00 MB every time. I don't understand why the expression totalMemoryMB := Round(totalMemory / 1MB, 2) is not being evaluated correctly

Can anyone see and explain what I'm doing wrong?
User avatar
CoffeeChaton
Posts: 44
Joined: 11 May 2024, 10:50

Re: Expression := or Round() not working as expected  Topic is solved

22 May 2024, 06:19

Code: Select all

#SingleInstance Force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode, Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir, %A_ScriptDir%  ; Ensures a consistent starting directory.

; Function to get total physical memory using Get-CimInstance
GetTotalPhysicalMemory()
{
    ; Define the PowerShell command
    psCommand := "powershell -command ""& { Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -ExpandProperty TotalPhysicalMemory }"""

    ; Run the PowerShell command and copy the result to the clipboard
    RunWait, %ComSpec% /c %psCommand% | clip, , Hide

    ; Retrieve the result from the clipboard
    ClipWait, 2
    totalMemory := Clipboard

    temp := StrReplace(Clipboard, "`r`n") + 0 ; "34263408640`r`n" -> "34263408640" -> 34263408640
    ; Convert the result from bytes to megabytes
    totalMemoryMB := Round(temp /1024/1024, 2)
    ;                           ^^^^^^^^^^^^^^^ bytes to 
    totalMemoryGB := Round(totalMemoryMB/1024, 2)


    MsgBox, % totalMemory "(bytes)" 
    MsgBox, % totalMemoryMB "(MB)"
    MsgBox, % totalMemoryGB "(GB)"

    return totalMemoryMB
}

; Example usage of the function
totalMemoryMB := GetTotalPhysicalMemory()
MsgBox, Total Physical Memory: %totalMemoryMB% MB

GuiClose:
ExitApp
RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: Expression := or Round() not working as expected

22 May 2024, 07:00

RickC wrote: I have a laptop with 8MB RAM, running AutoHotkey 1.1 (1.1.37.01) in Windows 10 (1809).

That, my friend, is impossible.

Minimum system requirements for Window 10 are 1GB for 32 bit and 2GB for 64 bit.

Russ
RickC
Posts: 302
Joined: 27 Oct 2013, 08:32

Re: Expression := or Round() not working as expected

22 May 2024, 07:07

So, I made two mistakes... 1) my 'rounding' maths operator and 2) not realising the result copied to the clipboard held both a return and a newline character in addition to the number.

Thank you, @CoffeeChaton, for your help. I was going round and round in circles.
Last edited by RickC on 22 May 2024, 13:59, edited 1 time in total.
RickC
Posts: 302
Joined: 27 Oct 2013, 08:32

Re: Expression := or Round() not working as expected

22 May 2024, 07:11

RussF wrote:
22 May 2024, 07:00
RickC wrote: I have a laptop with 8MB RAM, running AutoHotkey 1.1 (1.1.37.01) in Windows 10 (1809).

That, my friend, is impossible.

Minimum system requirements for Window 10 are 1GB for 32 bit and 2GB for 64 bit.

Russ
Oops... thanks for picking up the typo. I'll leave it there for the amusement of others. Must get some sleep... :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 248 guests