It is currently October 23rd, 2020, 10:54 am

Converting Bytes (AutoScale Alternative)

Tips and Tricks from the Rainmeter Community
kounger
Posts: 13
Joined: March 22nd, 2017, 12:14 am

Converting Bytes (AutoScale Alternative)

Post by kounger »

This is an alternative to AutoScale with the difference that the Byte value will be displayed with two digits after the decimal point. This is how Windows displays file and folder sizes.

Code: Select all

[Variables]
Bytes=8618111
;This will be converted to 8.21 MB

;Determines into which unit #Bytes# will be converted. This will be the first unit where the number value is smaller than 1024.
[CalcSizeConvClass]
Measure=Calc
Formula=(Floor(#Bytes# / 1024) = 0 ? 0 : (Floor(#Bytes# / (1024**2)) = 0 ? 1 : (Floor(#Bytes# / (1024**3)) = 0 ? 2 : (Floor(#Bytes# / (1024**4)) = 0 ? 3 : (Floor(#Bytes# / (1024**5)) = 0 ? 4 : (Floor(#Bytes# / (1024**6)) = 0 ? 5 : 6))))))
IfCondition=(CalcSizeConvClass = 0)
IfTrueAction=[!SetOption StringSizeUnit String "Bytes"]
IfCondition2=(CalcSizeConvClass = 1)
IfTrueAction2=[!SetOption StringSizeUnit String "KB"]
IfCondition3=(CalcSizeConvClass = 2)
IfTrueAction3=[!SetOption StringSizeUnit String "MB"]
IfCondition4=(CalcSizeConvClass = 3)
IfTrueAction4=[!SetOption StringSizeUnit String "GB"]
IfCondition5=(CalcSizeConvClass = 4)
IfTrueAction5=[!SetOption StringSizeUnit String "TB"]
IfCondition6=(CalcSizeConvClass = 5)
IfTrueAction6=[!SetOption StringSizeUnit String "PB"]
IfCondition7=(CalcSizeConvClass = 6)
IfTrueAction7=[!SetOption StringSizeUnit String "ERROR"][!Log "More than 1024 PB can't be displayed." Error]
DynamicVariables=1

;Bytes, KB, MB, GB, TB, PB or ERROR as defined by the IfConditions
[StringSizeUnit]
Measure=String
DynamicVariables=1

;CalcSizeConvClass defines how often #Bytes# has to be divided by 1024
[CalcSizeConversion]
Measure=Calc
Formula=#Bytes# / (1024 ** [CalcSizeConvClass])
DynamicVariables=1

;Rounded down to 2 digits after the decimal point.
;For 3 digits change both 100s to 1000s.
[CalcSizeRound]
Measure=Calc
Formula=Floor([CalcSizeConversion] * 100) / 100
DynamicVariables=1

;CalcSizeRound as string to not lose the two digits after the decimal point.
[CalcSizeRoundString]
Measure=String
String=[CalcSizeRound]
DynamicVariables=1

[MeterSize]
Meter=String
MeasureName=CalcSizeRoundString
MeasureName2=StringSizeUnit
X=10
Y=10
FontFace=Trebuchet MS
FontSize=9
FontColor=255,255,255,255
StringAlign=LEFT
AntiAlias=1
Text="Size: %1 %2"
Last edited by kounger on July 6th, 2018, 9:23 pm, edited 1 time in total.
User avatar
balala
Rainmeter Sage
Posts: 11590
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting Bytes (AutoScale Alternative)

Post by balala »

Although your code works well, I'm not sure at all it worth to use five measures to achieve what two options on a String meter can easily do. Beside the AutoScale, the number of decimals can also be set onto a String meter.
kounger
Posts: 13
Joined: March 22nd, 2017, 12:14 am

Re: Converting Bytes (AutoScale Alternative)

Post by kounger »

balala wrote:Although your code works well, I'm not sure at all it worth to use five measures to achieve what two options on a String meter can easily do. Beside the AutoScale, the number of decimals can also be set onto a String meter.
Oh. It even says so in the box that it lets you set the number of decimals along with it. :D Thanks for clearing that up. That's a more elegant solution of course.
User avatar
balala
Rainmeter Sage
Posts: 11590
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting Bytes (AutoScale Alternative)

Post by balala »

kounger wrote:Oh. It even says so in the box that it lets you set the number of decimals along with it. :D Thanks for clearing that up. That's a more elegant solution of course.
Don't misunderstand me, I don't want to subestimate your code, which works perfectly. Its only disadvantage is the size: it is too long for what it does.
Plus there is one more thing you have to know about: the Round function also can be used to get the same result (last function in this list).
But if you're a beginner with Rainmeter (what I think you are), I want to congratulate you, because your code definitely does what it is created for.
kounger
Posts: 13
Joined: March 22nd, 2017, 12:14 am

Re: Converting Bytes (AutoScale Alternative)

Post by kounger »

balala wrote:Don't misunderstand me, I don't want to subestimate your code, which works perfectly. Its only disadvantage is the size: it is too long for what it does.
Plus there is one more thing you have to know about: the Round function also can be used to get the same result (last function in this list).
But if you're a beginner with Rainmeter (what I think you are), I want to congratulate you, because your code definitely does what it is created for.
Yeah. I already replaced the code in my skin with the simpler solution. I don't know how I skipped over this when I read the instructions. I was so certain for some reason that this was not possible without some extra coding. :D Also the Formula with the Floor function actually does what I intended it to do. I just tried to copy what Windows does with file sizes and it actually rounds everything down while the Round function rounds up when the next digit is 5 or higher.
User avatar
balala
Rainmeter Sage
Posts: 11590
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting Bytes (AutoScale Alternative)

Post by balala »

kounger wrote:Also the Formula with the Floor function actually does what I intended it to do. I just tried to copy what Windows does with file sizes and it actually rounds everything down while the Round function rounds up when the next digit is 5 or higher.
Yeah, this is the essence of the Floor formula.
But don't be disappointed and keep working. Rainmeter has a lot of tricks which you can work with.
kounger
Posts: 13
Joined: March 22nd, 2017, 12:14 am

Re: Converting Bytes (AutoScale Alternative)

Post by kounger »

balala wrote:Yeah, this is the essence of the Floor formula.
But don't be disappointed and keep working. Rainmeter has a lot of tricks which you can work with.
Thanks for the motivation. I certainly will. :thumbup:
User avatar
balala
Rainmeter Sage
Posts: 11590
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting Bytes (AutoScale Alternative)

Post by balala »

kounger wrote:I certainly will.
:17good
User avatar
limitless
Posts: 64
Joined: January 8th, 2017, 2:31 am
Location: Charlotte, NC

Re: Converting Bytes (AutoScale Alternative)

Post by limitless »

I used this in my script, very good!!! I had an issue where I needed to set the Measure to the right size from bytes and didn't need to create a meter as I am outputting this info into a ToolTip. :thumbup:
Image
User avatar
ikarus1969
Posts: 393
Joined: February 28th, 2011, 3:20 pm
Location: Vienna, Austria

Re: Converting Bytes (AutoScale Alternative)

Post by ikarus1969 »

I want to throw in a solution provided by user smurfier in this thread/post about the recycle bin introducing the "scary math" for getting scale and the scaled size for a number of bytes.

a slightly adapted excerpt from the post:

Code: Select all

; Kilobyte (kB) = 1024
; Megabyte (MB) = 1048576 or 1024**2
; Gigabyte (GB) = 1073741824 or 1024**3
; Terabyte (TB) = 1099511627776 or 1024**4

[Variables]
Decimals=0

[Measure_Bin_Size]
Measure=Plugin
Plugin=RecycleManager
RecycleType=Size

[Measure_Scale]
Measure=CALC
Formula=Measure_Bin_Size = 0 ? 1 : CEIL(LOG(Measure_Bin_Size) / (10 * LOG(2)))
Substitute="1":"B","2":"kB","3":"mB","4":"gB","5":"tB","6":"pB","7":"eB","8":"zB","9":"yB"

[Measure_Scaled_Size]
Measure=CALC
Formula=ROUND(Measure_Bin_Size / 1024 **(Measure_Scale - 1), #Decimals#)