Jump to content
  • -1

Calculator File Size with MB


MahmoudBasem

Question

When I use this code in a Lua-supporting programming environment, it gives the correct result. However, when I use it in Game Guardian, it does not give the correct result and only provides an integer, not the fraction

Where is the error?

This is the code:

-- هذا مثال تخيلي لحجم الملف بالبايت (عندما يتم تحميله)

local downloadedFileSizeInBytes = 5780000 -- حجم الملف المنزل بالبايت (5.78 ميجابايت)

 

-- حساب الحجم بالميجابايت والكسر المتبقي

local fileSizeInMB = downloadedFileSizeInBytes / (1000 * 1000)

local remainderInBytes = downloadedFileSizeInBytes % (1000 * 1000)

 

-- تنسيق الحجم بالميجابايت والكسر للطباعة

local formattedSize = string.format("%.2f MB", fileSizeInMB)

 

print("File Size: " .. formattedSize)

IMG_20230919_225814.jpg

IMG_20230919_225856.jpg

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

From the documentation

Quote

Math is different from regular Lua. For binary operations, the first operand determines the type of result. For example,
3 / 2.0 will be 1, but 3.0 / 2.0 will be 1.5;
3 + 2.5 will be 5, but 2.5 + 3 will be 5.5.

So make sure to make first operand float, for example like: 

local fileSizeInMB = 1.0 * downloadedFileSizeInBytes / (1000 * 1000)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.