简体   繁体   English

JSON解码删除十进制尾随零

[英]JSON decoding removing decimal trailing zeros

I have a JsonArray and need to convert it into Array of objects.我有一个 JsonArray,需要将它转换为对象数组。 Below is the code I am using.下面是我正在使用的代码。 response - json response响应 - json 响应

ArrayList<TestClass> list = new ArrayList<>();
            JsonArray jsonArray = new JsonArray(response);
            jsonArray.forEach(o -> {
              JsonObject jsonObject = new JsonObject(o.toString());
              // logic to transform into desired class and add to list
            });
Json.encode(list.toArray());

Problem is when converting to string for decimals, trailing zeros are getting removed.问题是当转换为小数字符串时,尾随零被删除。 example : if value is 4.50 it's just returning 4.5 if it's 3.00, 3 is getting returned.示例:如果值为 4.50,则返回 4.5,如果为 3.00,则返回 3。 Looking to return raw data as it is.希望按原样返回原始数据。 Any pointers on fixing this有关修复此问题的任何指示

Trailing zeros can be printed if you treat decimal as String, for that you can use DecimalFormat or String.format(…) .如果您将十进制视为字符串,则可以打印尾随零,为此您可以使用DecimalFormatString.format(…) As we know adding trailing zero to a number after decimal doesn't impact the value of the number.正如我们所知,将尾随零添加到小数点后的数字不会影响数字的值。 If you are going to use the converted value in computation later in the program then better to not to worry about trailing zeros.如果您打算在程序稍后的计算中使用转换后的值,那么最好不要担心尾随零。 On the other hand if you're going to use this value to be displayed on screen somewhere then let the client handle it.另一方面,如果您要使用此值在屏幕上的某处显示,则让客户端处理它。

Java floating point types don't have a way to preserve a trailing zeros. Java 浮点类型无法保留尾随零。

For example, when you run this code snippet:例如,当您运行此代码片段时:

double d1 = 4.5;
double d2 = 4.50;
System.out.println(d1 == d2);

it will print true ... because 4.5 and 4.50 are the same number.它将打印true ... 因为4.54.50是相同的数字。 Both in Java, and in mathematics.无论是在 Java 中,还是在数学中。

And they also mean the same thing in JSON.它们在 JSON 中也表示相同的意思。

If you really want to preserve the trailing zeros, then:如果你真的想保留尾随零,那么:

  • In JSON you must represent the number as a string;在 JSON 中,您必须将数字表示为字符串; ie put quotes around it.即在它周围加上引号。
  • In Java you must store it in a String variable.在 Java 中,您必须将其存储在String变量中。 Which is going to make computation problematic.这将使计算成为问题。

Q: Would it be possible to faithfully preserve the trailing zeros AND do arithmetic?问:是否可以忠实地保留尾随零并进行算术运算?

A: I don't know of an existing Java type / class that does this.答:我不知道现有的 Java 类型/类可以执行此操作。

One of the problems is what happens with division.问题之一是分裂会发生什么。 For example, the exact representation of 1.0 / 3.0 would entail an infinite number of significant digits.例如, 1.0 / 3.0的精确表示需要无限数量的有效数字。 Which is problematic.这是有问题的。

I guess, if you had a mathematical model of floating point numbers that could represent a Real number and an uncertainty, AND that model was implementable (eg didn't involve problems such as indefinitely large representations, factorization, etc), then such a Java class would be possible.我想,如果您有一个浮点数的数学模型,可以表示数和不确定性,并且该模型是可实现的(例如,不涉及无限大的表示、因式分解等问题),那么这样的 Java类将是可能的。

But in the meantime, a better approach would be to review your requirement.但与此同时,更好的方法是审查您的要求。 Why do you need to preserve the trailing zeros?为什么需要保留尾随零? It doesn't make sense from a straight-forward mathematical perspective.从直接的数学角度来看,这是没有意义的。 Is there some other reason for doing it?这样做还有其他原因吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM