From 6dd545e4b56816269a3863c0324a2795d5119f5c Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 11 Oct 2021 18:20:05 +0200 Subject: [PATCH] adding the new helper extension for formatting file size --- app/build.gradle | 2 +- .../filemanager/pro/extensions/Long.kt | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/filemanager/pro/extensions/Long.kt diff --git a/app/build.gradle b/app/build.gradle index 7049d7f7..e1bac656 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -58,7 +58,7 @@ android { } dependencies { - implementation 'com.github.SimpleMobileTools:Simple-Commons:69346a62e0' + implementation 'com.github.SimpleMobileTools:Simple-Commons:cad50847e3' implementation 'com.github.Stericson:RootTools:df729dcb13' implementation 'com.github.Stericson:RootShell:1.6' implementation 'com.alexvasilkov:gesture-views:2.5.2' diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/extensions/Long.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/extensions/Long.kt new file mode 100644 index 00000000..61cee2fe --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/extensions/Long.kt @@ -0,0 +1,14 @@ +package com.simplemobiletools.filemanager.pro.extensions + +import java.text.DecimalFormat + +// use 1000 instead of 1024 at dividing +fun Long.formatSizeThousand(): String { + if (this <= 0) { + return "0 B" + } + + val units = arrayOf("B", "kB", "MB", "GB", "TB") + val digitGroups = (Math.log10(toDouble()) / Math.log10(1000.0)).toInt() + return "${DecimalFormat("#,##0.#").format(this / Math.pow(1000.0, digitGroups.toDouble()))} ${units[digitGroups]}" +}