0

I didn't see any example of reverting human-readable file size to an integer

There're a lot of functions which help to convert file size to human readble, such as: 1024 bytes -> 1KB. Here is an question about this: Converting file size in bytes to human-readable string

But I didn't see anywhere to support revert the process, such as: 1Kb -> 1024.

I recieved data from backend API, and it returns, for example 10MB, and I want to convert this String to an integer (bytes) to compare file sizes of multiple files. How to I do this in Javascript?

Tạ Anh Tú
  • 141
  • 1
  • 4
  • 1
    Can you show us what you have tried so far? – Peter Thoeny Jan 09 '23 at 06:40
  • It’s just base math. n KB = n * 1024 B, n MB = n * 1024 * 1024 B. You might want to round result to integer. What specific difficulty did you encounter and couldn’t overcome? – hackape Jan 09 '23 at 07:15
  • Avoid it if you can. Just store original size somewhere and than use it when needed. Otherwise split number and unit and multiply that number by value of the unit. B = 1, kB = 1024 and so on... – Jan Pfeifer Jan 09 '23 at 08:30

0 Answers0