文件大小最大两位小数显示

SDK 版本:HarmonyOS NEXT Developer Beta2 SDK (5.0.0.31)
DevEco-Studio 版本:DevEco Studio NEXT Developer Beta2 (5.0.3.502)
工程机版本:ALN-AL00 NEXT.0.0.31

文件大小最大两位小数显示

FileUtil.ets

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
44
45
/*
* Copyright (c) 2024. Dench.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { intl } from "@kit.LocalizationKit";

export class FileUtil {
/**
* 文件大小最大两位小数显示
*/
getFileSizeStr(size?: number): string {
const numberFormat = new intl.NumberFormat("zh-CN", {
maximumFractionDigits: 2,
});
if (size === undefined) {
return "";
}
if (size >= this.GB) {
return numberFormat.format(size / this.GB) + "G";
}
if (size >= this.MB) {
return numberFormat.format(size / this.MB) + "M";
}

if (size >= this.KB) {
return numberFormat.format(size / this.KB) + "k";
}
return size + "b";
}

private readonly KB = 1000;
private readonly MB = 1000000;
private readonly GB = 1000000000;
}

Reference

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/i18n-numbers-weights-measures-V5#%E6%95%B0%E5%AD%97%E6%A0%BC%E5%BC%8F%E5%8C%96

作者

Dench

发布于

2024-08-06

更新于

2024-08-06

许可协议

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×