diff options
Diffstat (limited to 'src/core/renderer/MCSizeFormatter.cpp')
-rw-r--r-- | src/core/renderer/MCSizeFormatter.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/core/renderer/MCSizeFormatter.cpp b/src/core/renderer/MCSizeFormatter.cpp new file mode 100644 index 00000000..f0823075 --- /dev/null +++ b/src/core/renderer/MCSizeFormatter.cpp @@ -0,0 +1,43 @@ +// +// MCSizeFormatter.cpp +// testUI +// +// Created by DINH Viêt Hoà on 1/29/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#include "MCSizeFormatter.h" + +#include <math.h> + +using namespace mailcore; + +String * SizeFormatter::stringWithSize(unsigned int size) +{ + double divider; + String * unit; + + if (size >= 1024 * 1024 * 1024) { + divider = 1024 * 1024 * 1024; + unit = MCLOCALIZEDSTRING(MCSTR("GB")); + } + else if (size >= 1024 * 1024) { + divider = 1024 * 1024; + unit = MCLOCALIZEDSTRING(MCSTR("MB")); + } + else if (size >= 1024) { + divider = 1024; + unit = MCLOCALIZEDSTRING(MCSTR("KB")); + } + else { + divider = 1; + unit = MCLOCALIZEDSTRING(MCSTR("bytes")); + } + + if ((size / divider) - round(size / divider) < 0.1) { + return String::stringWithUTF8Format("%.0f %s", size / divider, unit->UTF8Characters()); + } + else { + return String::stringWithUTF8Format("%.1f %s", size / divider, unit->UTF8Characters()); + } +} |