Skip to main content
Announcements
SYSTEM MAINTENANCE: Thurs., Sept. 19, 1 AM ET, Platform will be unavailable for approx. 60 minutes.
cancel
Showing results for 
Search instead for 
Did you mean: 
stephbzr
Contributor III
Contributor III

Convert bytes to kilobytes (tFileProperties)

Hello,

I would like to make a calculation on the data I get with the component tFileProperties. This one gives me the size of the file in bytes as below :

0695b00000UxfU0AAJ.png 

0695b00000UxfTRAAZ.png 

But I want to have it in kilobytes. To do this, I have to multiply the value by 0.001. But how to do the calculation?

The type of the column returned by the component (size) is Long

0695b00000UxfUtAAJ.png0695b00000UxfVDAAZ.pngI tried that, but it doesn't work

0695b00000UxgdwAAB.png

Labels (4)
1 Solution

Accepted Solutions
Anonymous
Not applicable

There are 1024 bytes in a kilobyte, so you need to divide your value by 1024 to get the kilobyte value.

 

Longs are whole numbers. They are essentially integers. Due to this, you need to cast them to doubles. To do this, do something like this....

 

((double)row5.size)/((double)1024L)

 

This will cast the size to a double and cast the 1024L (a hardcoded long....just as an example) to a double. The division of the double size and double 1024 will give you the answer you need.

View solution in original post

7 Replies
Anonymous
Not applicable

There are 1024 bytes in a kilobyte, so you need to divide your value by 1024 to get the kilobyte value.

 

Longs are whole numbers. They are essentially integers. Due to this, you need to cast them to doubles. To do this, do something like this....

 

((double)row5.size)/((double)1024L)

 

This will cast the size to a double and cast the 1024L (a hardcoded long....just as an example) to a double. The division of the double size and double 1024 will give you the answer you need.

stephbzr
Contributor III
Contributor III
Author

Thank you very much for your clear answer @Richard Hall​ . For information, I found the formula for converting bytes to kilobytes here : Convert Bytes to kB - Bytes to Kilobytes Calculator (dataunitconverter.com)

Yours is correct, I tested both and there is a small difference (62.3 dividing by 1024 and 63.8 multiplying by 0.001).

Here is my file: 

 

0695b00000Uxi7TAAR.pngIs there a big importance to add L after 1024? what does it mean?

 

I would now like to round this result, I tried the functions round() and Math.round(((double)input_row.size)/((double)1024L));

But without success

Anonymous
Not applicable

Not a problem @Stéphane Barbezier​. Computers use base 2. This is because the smallest unit (bit) is capable of holding 2 states (0 or 1). There are 8 bits in a byte (00000000 = 0 and 11111111 = 255 ....so there are 256 possible numbers in a byte). There are 1024 bytes in a kilobyte. However, some companies have made this simpler for people who use base 10 (most of us) and have converted that 1000. It is best to stick to 1024 in my opinion.

 

The letter L after the hardcoded number tells the processor that the hardcoded number should be considered a long and not an int. That is all it is for.

 

What are you trying to round to? A whole number or a number of decimal places?

 

An example of code to round to 2 decimal places can be seen below.....

 

(new java.text.DecimalFormat("0.00")).format(((double)input_row.size)/((double)1024L))

 

This should work, but you'll need to test it to be sure.

stephbzr
Contributor III
Contributor III
Author

Thanks again @Richard Hall​  for the very interesting explanations!

I'm just trying to convert my decimal value obtained for kilobytes to integer without decimal.

With your code I get the following error :

 

0695b00000UxjOcAAJ.png0695b00000UxjQHAAZ.pngType mismatch : cannot convert from String to Double

 

I want to obtain for example 62 instead of 62.3 or 64 instead of 63.8

stephbzr
Contributor III
Contributor III
Author

@Richard Hall​  Searching in other forums, I made a solution that is close to what I want, here it is: 

 

Double.parseDouble(String.format("%.0f",((double)input_row.size)/((double)1024L)).replace(",", "."));

 

Now I have 62.0 for example

if 5f then I would have 5 decimals after the 62.

 

But I would like to get just 62. Any idea? 

Anonymous
Not applicable

Ah, sorry. I assumed that you wanted to display the figure. When you truncate or round a number like above, it formats it as a String. You can fix this by changing your column type (on the right) to String.

 

If you want to round the number to a whole number (rounding up to the next whole number or down, depending on >=0.5 or <0.5) then Math.round is the method you need.

stephbzr
Contributor III
Contributor III
Author

Indeed, I was searching without knowing in which type I want my final result. Looking at the database column that should store my result, it is of type string with two decimals. Finally, the following code satisfies me perfectly:

 

String.format("%.2f",((double)input_row.size)/((double)1024L))

 

It's true that having the result in integer type would have interested me also for my personal practice, but I'll look for another time. 

 

Thank you very much for your help @Richard Hall​ !