“JS MB a bytes” Código de respuesta

La función JavaScript convierte bytes en MB

function bytesToSize(bytes) {
  const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
  if (bytes === 0) return 'n/a'
  const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10)
  if (i === 0) return `${bytes} ${sizes[i]})`
  return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`
}
Tender Teira

byte a MB JS

function formatBytes(a,b=2){if(0===a)return"0 Bytes";const c=0>b?0:b,d=Math.floor(Math.log(a)/Math.log(1024));return parseFloat((a/Math.pow(1024,d)).toFixed(c))+" "+["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"][d]}
Colorful Cowfish

JS MB a bytes

function toBytes(size, type)
{
  const types = ["B", "KB", "MB", "GB", "TB"];

  const key = types.indexOf(type.toUpperCase())
  
  if (typeof key !== "boolean") {
      return  size * 1024 ** key;
  }
  return "invalid type: type must be GB/KB/MB etc.";
}
TP

Respuestas similares a “JS MB a bytes”

Preguntas similares a “JS MB a bytes”

Más respuestas relacionadas con “JS MB a bytes” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código