It is currently November 15th, 2018, 7:17 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)

kounger » July 6th, 2018, 8:08 pm

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: 6974
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting Bytes (AutoScale Alternative)

balala » July 6th, 2018, 8:25 pm

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)

kounger » July 6th, 2018, 9:23 pm

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: 6974
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting Bytes (AutoScale Alternative)

balala » July 7th, 2018, 6:09 am

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)

kounger » July 7th, 2018, 9:20 pm

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: 6974
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting Bytes (AutoScale Alternative)

balala » July 7th, 2018, 9:35 pm

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)

kounger » July 7th, 2018, 9:55 pm

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: 6974
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting Bytes (AutoScale Alternative)

balala » July 8th, 2018, 11:46 am

kounger wrote:I certainly will.
:17good