2 * Copyright (C) 2006 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package android.text.format;
19 import android.content.Context;
22 * Utility class to aid in formatting common values that are not covered
23 * by the standard java.util.Formatter.
25 public final class Formatter {
28 * Formats a content size to be in the form of bytes, kilobytes, megabytes, etc
30 * @param context Context to use to load the localized units
31 * @param number size value to be formated
32 * @return formated string with the number
34 public static String formatFileSize(Context context, long number) {
35 return formatFileSize(context, number, false);
39 * Like {@link #formatFileSize}, but trying to generate shorter numbers
40 * (showing fewer digits of precisin).
42 public static String formatShortFileSize(Context context, long number) {
43 return formatFileSize(context, number, true);
46 private static String formatFileSize(Context context, long number, boolean shorter) {
47 if (context == null) {
51 float result = number;
52 int suffix = com.android.internal.R.string.byteShort;
54 suffix = com.android.internal.R.string.kilobyteShort;
55 result = result / 1024;
58 suffix = com.android.internal.R.string.megabyteShort;
59 result = result / 1024;
62 suffix = com.android.internal.R.string.gigabyteShort;
63 result = result / 1024;
66 suffix = com.android.internal.R.string.terabyteShort;
67 result = result / 1024;
70 suffix = com.android.internal.R.string.petabyteShort;
71 result = result / 1024;
75 value = String.format("%.2f", result);
76 } else if (result < 10) {
78 value = String.format("%.1f", result);
80 value = String.format("%.2f", result);
82 } else if (result < 100) {
84 value = String.format("%.0f", result);
86 value = String.format("%.2f", result);
89 value = String.format("%.0f", result);
91 return context.getResources().
92 getString(com.android.internal.R.string.fileSizeSuffix,
93 value, context.getString(suffix));
97 * Returns a string in the canonical IP format ###.###.###.### from a packed integer containing
98 * the IP address. The IP address is expected to be in little-endian format (LSB first). That
99 * is, 0x01020304 will return "4.3.2.1".
101 * @param addr the IP address as a packed integer with LSB first.
102 * @return string with canonical IP address format.
104 public static String formatIpAddress(int addr) {
105 StringBuffer buf = new StringBuffer();
106 buf.append(addr & 0xff).append('.').
107 append((addr >>>= 8) & 0xff).append('.').
108 append((addr >>>= 8) & 0xff).append('.').
109 append((addr >>>= 8) & 0xff);
110 return buf.toString();