Converting Bytes (AutoScale Alternative)

Tips and Tricks from the Rainmeter Community

Converting Bytes (AutoScale Alternative)

July 6th, 2018, 8:08 pm
kounger
   [13 posts]

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

Re: Converting Bytes (AutoScale Alternative)

July 6th, 2018, 8:25 pm
balala
Rainmeter Sage   [6678 posts]

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.

Re: Converting Bytes (AutoScale Alternative)

July 6th, 2018, 9:23 pm
kounger
   [13 posts]

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

Re: Converting Bytes (AutoScale Alternative)

July 7th, 2018, 6:09 am
balala
Rainmeter Sage   [6678 posts]

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.

Re: Converting Bytes (AutoScale Alternative)

July 7th, 2018, 9:20 pm
kounger
   [13 posts]

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

Re: Converting Bytes (AutoScale Alternative)

July 7th, 2018, 9:35 pm
balala
Rainmeter Sage   [6678 posts]

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.

Re: Converting Bytes (AutoScale Alternative)

July 7th, 2018, 9:55 pm
kounger
   [13 posts]

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

Re: Converting Bytes (AutoScale Alternative)

July 8th, 2018, 11:46 am
balala
Rainmeter Sage   [6678 posts]

kounger wrote:I certainly will.
:17good

Return to “Community Tips and Tricks”



Who is online

Users browsing this forum: No registered users and 1 guest