fix #2177 use number formatter
This commit is contained in:
parent
4213e479e5
commit
e07d0abeb5
4 changed files with 69 additions and 17 deletions
|
@ -23,6 +23,7 @@ import java.nio.file.Files;
|
|||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
@ -154,25 +155,49 @@ public class FessFunctions {
|
|||
return date.format(DateTimeFormatter.ofPattern(Constants.ISO_DATETIME_FORMAT, Locale.ROOT));
|
||||
}
|
||||
|
||||
public static String formatNumber(final long value) {
|
||||
int ratio = 1;
|
||||
String unit = "";
|
||||
public static String formatNumber(final long value, final String pattern) {
|
||||
final DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(getUserLocale());
|
||||
df.applyPattern(pattern);
|
||||
return df.format(value);
|
||||
}
|
||||
|
||||
private static Locale getUserLocale() {
|
||||
final Locale locale = ComponentUtil.getRequestManager().getUserLocale();
|
||||
if (locale == null) {
|
||||
return Locale.ROOT;
|
||||
}
|
||||
return locale;
|
||||
}
|
||||
|
||||
public static String formatFileSize(final long value) {
|
||||
double target = (double) value;
|
||||
String unit = ""; // TODO l10n?
|
||||
String format = "0.#";
|
||||
if (value < 1024) {
|
||||
format = "0";
|
||||
} else if (value < (1024 * 1024)) {
|
||||
ratio = 1024;
|
||||
} else if (value < 1024L * 1024L) {
|
||||
target /= 1024;
|
||||
unit = "K";
|
||||
} else if (value < (1024 * 1024 * 1024)) {
|
||||
ratio = 1024 * 1024;
|
||||
} else if (value < 1024L * 1024L * 1024L) {
|
||||
target /= 1024;
|
||||
target /= 1024;
|
||||
unit = "M";
|
||||
} else {
|
||||
ratio = 1024 * 1024 * 1024;
|
||||
} else if (value < 1024L * 1024L * 1024L * 1024L) {
|
||||
target /= 1024;
|
||||
target /= 1024;
|
||||
target /= 1024;
|
||||
unit = "G";
|
||||
} else {
|
||||
target /= 1024;
|
||||
target /= 1024;
|
||||
target /= 1024;
|
||||
target /= 1024;
|
||||
unit = "T";
|
||||
}
|
||||
final DecimalFormat df = new DecimalFormat(format + unit);
|
||||
final DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(getUserLocale());
|
||||
df.applyPattern(format);
|
||||
df.setRoundingMode(RoundingMode.HALF_UP);
|
||||
return df.format((double) value / ratio);
|
||||
return df.format(target) + unit;
|
||||
}
|
||||
|
||||
public static String pagingQuery(final String query) {
|
||||
|
|
|
@ -71,15 +71,27 @@
|
|||
</example>
|
||||
</function>
|
||||
|
||||
<function>
|
||||
<description>
|
||||
Returns formatted number as a file size from a given value.
|
||||
</description>
|
||||
<name>formatFileSize</name>
|
||||
<function-class>org.codelibs.fess.taglib.FessFunctions</function-class>
|
||||
<function-signature>java.lang.String formatFileSize(long)</function-signature>
|
||||
<example>
|
||||
${fe:formatFileSize(doc.content_length)}
|
||||
</example>
|
||||
</function>
|
||||
|
||||
<function>
|
||||
<description>
|
||||
Returns formatted number from a given value.
|
||||
</description>
|
||||
<name>formatNumber</name>
|
||||
<function-class>org.codelibs.fess.taglib.FessFunctions</function-class>
|
||||
<function-signature>java.lang.String formatNumber(long)</function-signature>
|
||||
<function-signature>java.lang.String formatNumber(long, java.lang.String)</function-signature>
|
||||
<example>
|
||||
${fe:formatNumber(doc.content_length)}
|
||||
${fe:formatNumber(doc.content_length, "###,###")}
|
||||
</example>
|
||||
</function>
|
||||
|
||||
|
|
|
@ -5,12 +5,12 @@
|
|||
<p>
|
||||
<c:if test="${allRecordCountRelation=='EQUAL_TO'}">
|
||||
<la:message key="labels.search_result_status"
|
||||
arg0="${displayQuery}" arg1="${f:h(allRecordCount)}"
|
||||
arg0="${displayQuery}" arg1="${fe:formatNumber(allRecordCount,'###,###')}"
|
||||
arg2="${f:h(currentStartRecordNumber)}"
|
||||
arg3="${f:h(currentEndRecordNumber)}" />
|
||||
</c:if><c:if test="${allRecordCountRelation!='EQUAL_TO'}">
|
||||
<la:message key="labels.search_result_status_over"
|
||||
arg0="${displayQuery}" arg1="${f:h(allRecordCount)}"
|
||||
arg0="${displayQuery}" arg1="${fe:formatNumber(allRecordCount,'###,###')}"
|
||||
arg2="${f:h(currentStartRecordNumber)}"
|
||||
arg3="${f:h(currentEndRecordNumber)}" />
|
||||
</c:if>
|
||||
|
@ -69,7 +69,7 @@
|
|||
<small class="d-none d-lg-inline-block"> <la:link
|
||||
href="/search?q=${f:u(q)}&ex_q=${f:u(queryEntry.value)}&sdh=${f:u(fe:sdh(doc.similar_docs_hash))}${fe:facetQuery()}${fe:geoQuery()}">
|
||||
<la:message key="labels.search_result_similar"
|
||||
arg0="${fe:formatNumber(doc.similar_docs_count-1)}" />
|
||||
arg0="${fe:formatFileSize(doc.similar_docs_count-1)}" />
|
||||
</la:link>
|
||||
</small>
|
||||
</c:if>
|
||||
|
@ -103,7 +103,7 @@
|
|||
</c:if>
|
||||
<c:set var="hasInfo" value="true" />
|
||||
<la:message key="labels.search_result_size"
|
||||
arg0="${fe:formatNumber(doc.content_length)}" />
|
||||
arg0="${fe:formatFileSize(doc.content_length)}" />
|
||||
</c:if> <c:if test="${searchLogSupport}">
|
||||
<c:if test="${hasInfo}">
|
||||
<div class="d-sm-none"></div>
|
||||
|
|
|
@ -20,6 +20,21 @@ import java.util.Date;
|
|||
import org.codelibs.fess.unit.UnitFessTestCase;
|
||||
|
||||
public class FessFunctionsTest extends UnitFessTestCase {
|
||||
public void test_formatNumber() {
|
||||
assertEquals("0", FessFunctions.formatNumber(0, "###,###"));
|
||||
assertEquals("1,000", FessFunctions.formatNumber(1000, "###,###"));
|
||||
assertEquals("1,000,000", FessFunctions.formatNumber(1000000, "###,###"));
|
||||
}
|
||||
|
||||
public void test_formatFileSize() {
|
||||
assertEquals("0", FessFunctions.formatFileSize(0));
|
||||
assertEquals("1000", FessFunctions.formatFileSize(1000));
|
||||
assertEquals("976.6K", FessFunctions.formatFileSize(1000000));
|
||||
assertEquals("953.7M", FessFunctions.formatFileSize(1000000000));
|
||||
assertEquals("931.3G", FessFunctions.formatFileSize(1000000000000L));
|
||||
assertEquals("909.5T", FessFunctions.formatFileSize(1000000000000000L));
|
||||
}
|
||||
|
||||
public void test_parseDate() {
|
||||
Date date;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue