You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2021/02/06 10:57:19 UTC

[freemarker] branch 3 updated: Forward ported from 2.3-gae: FREEMARKER-169: "computer" number format now formats infinity and NaN the same way as ?c does.

This is an automated email from the ASF dual-hosted git repository.

ddekany pushed a commit to branch 3
in repository https://gitbox.apache.org/repos/asf/freemarker.git


The following commit(s) were added to refs/heads/3 by this push:
     new 17514e8  Forward ported from 2.3-gae: FREEMARKER-169: "computer" number format now formats infinity and NaN the same way as ?c does.
17514e8 is described below

commit 17514e8d57e31cd4da8099813eb0a10558c4a9f8
Author: ddekany <dd...@apache.org>
AuthorDate: Sat Feb 6 11:52:14 2021 +0100

    Forward ported from 2.3-gae: FREEMARKER-169: "computer" number format now formats infinity and NaN the same way as ?c does.
---
 .../core/valueformat/NumberFormatTest.java         | 24 +++++++++++++++++++++-
 .../org/apache/freemarker/core/Environment.java    |  4 ++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/freemarker-core-test/src/test/java/org/apache/freemarker/core/valueformat/NumberFormatTest.java b/freemarker-core-test/src/test/java/org/apache/freemarker/core/valueformat/NumberFormatTest.java
index 3b21a35..e5778e3 100644
--- a/freemarker-core-test/src/test/java/org/apache/freemarker/core/valueformat/NumberFormatTest.java
+++ b/freemarker-core-test/src/test/java/org/apache/freemarker/core/valueformat/NumberFormatTest.java
@@ -371,5 +371,27 @@ public class NumberFormatTest extends TemplateTest {
         }
         
     }
-    
+
+    @Test
+    public void testCFormatOfSpecialNumbers() throws IOException, TemplateException {
+        addToDataModel("pInf", Double.POSITIVE_INFINITY);
+        addToDataModel("nInf", Double.NEGATIVE_INFINITY);
+        addToDataModel("nan", Double.NaN);
+
+        String humanAudienceOutput = "\u221e -\u221e \ufffd";
+        String computerAudienceOutput = "INF -INF NaN";
+
+        assertOutput("${pInf?c} ${nInf?c} ${nan?c}", computerAudienceOutput);
+        assertOutput("<#setting numberFormat='computer'>${pInf} ${nInf} ${nan}",  computerAudienceOutput);
+        assertOutput("${pInf} ${nInf} ${nan}", humanAudienceOutput);
+
+        Environment env = new Template(null, "", getConfiguration())
+                .createProcessingEnvironment(null, null);
+        assertEquals(
+                computerAudienceOutput,
+                env.getCNumberFormat().format(Double.POSITIVE_INFINITY)
+                        + " " + env.getCNumberFormat().format(Double.NEGATIVE_INFINITY)
+                        + " " + env.getCNumberFormat().format(Double.NaN));
+    }
+
 }
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/Environment.java b/freemarker-core/src/main/java/org/apache/freemarker/core/Environment.java
index ddddf4b..daf9167 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/Environment.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/Environment.java
@@ -123,6 +123,10 @@ public final class Environment extends MutableProcessingConfiguration<Environmen
         C_NUMBER_FORMAT = new DecimalFormat("0.################", new DecimalFormatSymbols(Locale.US));
         C_NUMBER_FORMAT.setGroupingUsed(false);
         C_NUMBER_FORMAT.setDecimalSeparatorAlwaysShown(false);
+        DecimalFormatSymbols symbols = C_NUMBER_FORMAT.getDecimalFormatSymbols();
+        symbols.setInfinity("INF");
+        symbols.setNaN("NaN");
+        C_NUMBER_FORMAT.setDecimalFormatSymbols(symbols);
     }
 
     private final Configuration configuration;