aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/renderer/MCSizeFormatter.cc
blob: f08230754ef5e9d0a631e9b67212bb6f3daec218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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());
    }
}