Round to 2 decimal places?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
RNIB
Macro Veteran
Posts: 159
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Round to 2 decimal places?

Post by RNIB » Fri Nov 24, 2017 2:08 pm

I've got a script that gets the names of all folders and their size and pastes that into Notepad. The file size is reported in MB for folders under 1GB in size and reported in GB for folders larger than 1GB.

Code: Select all


Srt>folder
Let>INPUT_BROWSE=2
Input>folder_result,Please Select The Root Folder,
IF>folder_result=
Exit>0
EndIF
Let>GFL_TYPE=1
Let>GFL_SORTTYPE=3
GetFileList>%folder_result%\*.*,folders
Separate>folders,;,folder_names


RunProgram>C:\Windows\System32\notepad.exe
WaitWindowOpen>Untitled - Notepad
Send>Names of folder in %folder_result%
Press>Enter*2
Let>k=0
Repeat>k
Let>k=k+1
ExtractFileName>folder_names_%k%,name_folder
VBSTART
  Function GetFolderSize(Path)
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objFolder = objFSO.GetFolder(Path)
     GetFolderSize = objFolder.Size
  End Function
VBEND
VBEval>GetFolderSize("%folder_result%\%name_folder%"),fSize
If>fSize<1073741824
Let>MBSize={round(%fSize%/1048576)}
Else
Let>GBSize={round(%fSize%/1073741824)}
EndIF
Wait>0.2
PutClipBoard>name_folder
Press>Ctrl
Send>>v
Release>Ctrl
Press>Tab
Send>-
Press>Tab
If>fSize<1073741824
Send>%MBSize%MB
Else
Send>%GBSize%GB
Endif
Press>Enter
Until>k=folder_names_count
END>folder
If I don't use the round function then I can end up with results like 32.348291MB but with the round function I currently end up with 32MB. What I'd like is to just have it to two decimal places e.g. 32.34MB

It doesn't really need to be 'intelligent' and round up/down to the nearest two decimal places (i.e. in the above example it would return 32.35MB) if that's too complex but it would be a useful advantage if it did. Failing that just chopping off everything after the first 2 decimal places would suffice but I assume that the total number of decimal places may vary.

I've tried using different Round Modes but I always end up with a whole number and I've tried using trunc function but the result is always the same but then I don't really understand the help file on these functions.

Is there a way of achieving this?

RNIB
Macro Veteran
Posts: 159
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Re: Round to 2 decimal places?

Post by RNIB » Fri Nov 24, 2017 3:06 pm

Solved it. Well, I didn't solve it but I found someone elses code on here that did it viewtopic.php?f=9&t=7974&hilit=decimal+places

So I now have:

Code: Select all

Srt>folder
Let>INPUT_BROWSE=2
Input>folder_result,Please Select The Root Folder,
IF>folder_result=
Exit>0
EndIF
Let>GFL_TYPE=1
Let>GFL_SORTTYPE=3
GetFileList>%folder_result%\*.*,folders
Separate>folders,;,folder_names


RunProgram>C:\Windows\System32\notepad.exe
WaitWindowOpen>Untitled - Notepad
Send>Names of folder in %folder_result%
Press>Enter*2
Let>k=0
Repeat>k
Let>k=k+1
ExtractFileName>folder_names_%k%,name_folder
VBSTART
  Function GetFolderSize(Path)
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objFolder = objFSO.GetFolder(Path)
     GetFolderSize = objFolder.Size
  End Function
VBEND
VBEval>GetFolderSize("%folder_result%\%name_folder%"),fSize
If>fSize<1073741824
Let>MBSize=fSize/1048576
//Change this to the number of decimal places you want
Let>NumDecimals=2
//Mess around with this...
Let>ValueToRound=MBSize
// Set Multiplier as 10, 100, 1000 etc.
Let>Multiplier={Power(10,%NumDecimals%)}
Let>NewValue={Round(%ValueToRound%*%Multiplier%)}
Let>MBValue=NewValue/Multiplier
Else
Let>GBSize=fSize/1073741824
//Change this to the number of decimal places you want
Let>NumDecimals=2
//Mess around with this...
Let>ValueToRound=GBSize
// Set Multiplier as 10, 100, 1000 etc.
Let>Multiplier={Power(10,%NumDecimals%)}
Let>NewValue={Round(%ValueToRound%*%Multiplier%)}
Let>GBValue=NewValue/Multiplier
EndIF
Wait>0.2
PutClipBoard>name_folder
Press>Ctrl
Send>>v
Release>Ctrl
Press>Tab
Send>-
Press>Tab
If>fSize<1073741824
Send>%MBValue%MB
Else
Send>%GBValue%GB
Endif
Press>Enter
Until>k=folder_names_count
END>folder

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Round to 2 decimal places?

Post by JRL » Sat Nov 25, 2017 5:23 am

There is now a Format> function. Performs a number of miracles including rounding to a specified number of decimal places.

Also pulled the VBSTART--> VBEND out of the loop. The block only needs to be called once to be active.

Code: Select all

VBSTART
  Function GetFolderSize(Path)
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objFolder = objFSO.GetFolder(Path)
     GetFolderSize = objFolder.Size
  End Function
VBEND

GoSub>folder

Srt>folder
Let>INPUT_BROWSE=2
Input>folder_result,Please Select The Root Folder,
IF>folder_result=
Exit>0
EndIF
Let>GFL_TYPE=1
Let>GFL_SORTTYPE=3
GetFileList>%folder_result%\*.*,folders
Separate>folders,;,folder_names
RunProgram>C:\Windows\System32\notepad.exe
WaitWindowOpen>Untitled - Notepad
Send>Names of folder in %folder_result%
Press>Enter*2
Let>k=0
Repeat>k
Let>k=k+1
ExtractFileName>folder_names_%k%,name_folder
VBEval>GetFolderSize("%folder_result%\%name_folder%"),fSize
If>fSize<1073741824
Let>MBSize=fSize/1048576
//Change the number between the dot and the "n" the number of decimal places you want
Format>%.2n,MBSize,MBValue
Else
Let>GBSize=fSize/1073741824
//Change the number between the dot and the "n" the number of decimal places you want
Format>%.2n,GBSize,GBValue
EndIF
Wait>0.2
PutClipBoard>name_folder
Press>Ctrl
Send>>v
Release>Ctrl
Press>Tab
Send>-
Press>Tab
If>fSize<1073741824
Send>%MBValue%MB
Else
Send>%GBValue%GB
Endif
Press>Enter
Until>k=folder_names_count
END>folder

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts