简体   繁体   English

如何使用java(spring boot)以json格式检索完整值(带小数的大整数值)?

[英]How to retrieve full value as its(big integer value with decimals) as in json format by using java (spring boot)?

I am created a rest API in spring boot which consumes third party API response and return the response.我在 Spring Boot 中创建了一个 rest API,它使用第三方 API 响应并返回响应。

Challenges : Third party API response {"price":123456789123456789123456789.05}挑战:第三方 API 响应{"price":123456789123456789123456789.05}

I want to get the "price" from the single object ,So it will return 123456789123456789123456789.05 as expected我想从单个对象中获取“价格”,因此它将按预期返回 123456789123456789123456789.05

Expected Output : 123456789123456789123456789.05预期输出: 123456789123456789123456789.05

Actual Output : 1.2345678912345679e+26实际输出: 1.2345678912345679e+26

Note:笔记:

The actual output will given as exponential format but i want actual data without exponential.实际输出将以指数格式给出,但我想要没有指数的实际数据。

Code: pom.xml代码: pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sample</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- tag::actuator[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- end::actuator[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

SampleApplication.java示例应用程序.java

package com.example.sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


@SpringBootApplication
public class SampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

HelloController.java你好控制器.java

package com.example.sample;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController {
 @Autowired
 RestTemplate restTemplate;

 @RequestMapping("/")
 public double index() throws Exception {
  HttpHeaders headers = new HttpHeaders();
  headers.set("token", "rtp");
  HttpEntity entity = new HttpEntity < String > ("parameters", headers);
  String END_Point_Test_API = "http://localhost:8080/api/test";
  ResponseEntity < String > responseData = restTemplate.exchange(END_Point_Test_API, HttpMethod.GET, entity, String.class);
  //Third party response {"price":123456789123456789123456789.05}
  System.out.println(responseData.getBody());
  String data = responseData.getBody();

  JSONParser parser = new JSONParser();
  JSONObject jsobj = (JSONObject) parser.parse(data);
  //double price =(double)jsobj.get("price");
  double price = Double.parseDouble(String.valueOf(jsobj.get("price")));
  return price;

 }

}

The correct way to convert BigDecimal to double is:将 BigDecimal 转换为 double 的正确方法是:

((BigDecimal) jsobj.get("price")).doubleValue()

However, you are converting BigDecimal to double.但是,您正在将 BigDecimal 转换为 double。 Double has a vast range, but even this range has limits. Double 的范围很广,但即使是这个范围也有限制。 When you exceed it you will run into conversion problems.当您超过它时,您将遇到转换问题。

My suggested answer is, do not convert it to double use BigDecimal for your price.我建议的答案是,不要以您的价格将其转换为双重使用 BigDecimal。

To make it clear: as soon as double is used, you have an approximation of the actual value, without defined precision and everything will just be some imperfect repair.明确地说:一旦使用double ,您就有了实际值的近似值,没有定义的精度,一切都只是一些不完美的修复。

BigDecimal price = (BigDecimal) jsobj.get("price");
System.out.println(price.toPlainString());

This is probably also mentioned in the provided links.提供的链接中可能也提到了这一点。 toPlainString will avoid using the scientific E-notation. toPlainString将避免使用科学的 E 符号。

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

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