具有惯用Scala的人类可读格式的字节

具有惯用Scala的人类可读格式的字节,scala,byte,string-formatting,Scala,Byte,String Formatting,我正在寻找惯用的Scala代码,它将大量字节格式化为人类可读的字符串,这在向用户显示文件大小时非常有用 例如,应将1000字节格式化为1.0 kB,将49134421234字节格式化为49.1 GB 格式化功能的一些要求: 尽可能的可读和惯用 适用于SI(如兆字节)和IEC单位(如兆字节) 不依赖于外部库 可在浏览器中通过 我的版本: /** * Converts a number of bytes into a human-readable string * such as `2.2

我正在寻找惯用的Scala代码,它将大量字节格式化为人类可读的字符串,这在向用户显示文件大小时非常有用

例如,应将1000字节格式化为
1.0 kB
,将49134421234字节格式化为
49.1 GB

格式化功能的一些要求:

  • 尽可能的可读和惯用
  • 适用于SI(如兆字节)和IEC单位(如兆字节)
  • 不依赖于外部库
  • 可在浏览器中通过
我的版本:

/**
  * Converts a number of bytes into a human-readable string
  * such as `2.2 MB` or `8.0 EiB`.
  *
  * @param bytes the number of bytes we want to convert
  * @param si    if true, we use base 10 SI units where 1000 bytes are 1 kB.
  *              If false, we use base 2 IEC units where 1024 bytes are 1 KiB.
  * @return the bytes as a human-readable string
  */
def humanReadableSize(bytes: Long, si: Boolean): String = {

  // See https://en.wikipedia.org/wiki/Byte
  val (baseValue, unitStrings) =
    if (si)
      (1000, Vector("B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"))
    else
      (1024, Vector("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"))

  def getExponent(curBytes: Long, baseValue: Int, curExponent: Int = 0): Int =
    if (curBytes < baseValue) curExponent
    else {
      val newExponent = 1 + curExponent
      getExponent(curBytes / (baseValue * newExponent), baseValue, newExponent)
    }

  val exponent = getExponent(bytes, baseValue)
  val divisor = Math.pow(baseValue, exponent)
  val unitString = unitStrings(exponent)

  // Divide the bytes and show one digit after the decimal point
  f"${bytes / divisor}%.1f $unitString"
}

我找到了这个替代版本,虽然它不像上面那样有选择,但它快速而简单

def bytesToString(大小:长):字符串={
val TB=1L=2*KB){
(大小为[Double]/KB,“KB”)
}否则{
(尺寸为[双],“B”)
}
}
“%.1f%s”.formatLocal(Locale.US,值,单位)}

我认为,如果将
size.asInstanceOf[Double]
放入一个变量中,而不是重复五次,将有助于提高可读性。
// Result: 1.0 kB
humanReadableSize(1000, si = true)

// Result: 1000.0 B
humanReadableSize(1000, si = false)

// Result: 10.0 kB
humanReadableSize(10000, si = true)

// Result: 9.8 KiB
humanReadableSize(10000, si = false)

// Result: 49.1 GB
humanReadableSize(49134421234L, si = true)

// Result: 45.8 GiB
humanReadableSize(49134421234L, si = false)
def bytesToString(size: Long): String = {
val TB = 1L << 40
val GB = 1L << 30
val MB = 1L << 20
val KB = 1L << 10

val (value, unit) = {
  if (size >= 2*TB) {
    (size.asInstanceOf[Double] / TB, "TB")
  } else if (size >= 2*GB) {
    (size.asInstanceOf[Double] / GB, "GB")
  } else if (size >= 2*MB) {
    (size.asInstanceOf[Double] / MB, "MB")
  } else if (size >= 2*KB) {
    (size.asInstanceOf[Double] / KB, "KB")
  } else {
    (size.asInstanceOf[Double], "B")
  }
}
"%.1f %s".formatLocal(Locale.US, value, unit)}