0
点赞
收藏
分享

微信扫一扫

python爬虫基础----字符串高级

爱薇Ivy趣闻 22小时前 阅读 0

1、背景

在使用spring boot时,前端的界面展示的数据是2 ,在数据库中存储的是小数。但是导出Excel的时候数据是 2.00 。奇了怪了为啥会不一样,数据都是一样的没有做过处理。

2、排查问题

经过层层的debug 发现数据库返回的数据是2.00,写入Excel的数据就是查询数据库原始的数据。

List<Map<String, Object>> data = jdbcTemplate.queryForObject(sql);

前端界面的接口调用同样的方法,查询的SQL返回的值data是一样的,但是在浏览器F12模式下看接口返回的值是2。并没有.00 ,说明spring boot在返回JSON数据的时候动了手脚。

3、解决方法

方法1:

  如果前端对数据的格式不敏感,建议全部修改为字符串返回

spring:
  jackson:
    ## 日期格式(可根据自己的需求修改格式)
    date-format: yyyy-MM-dd HH:mm:ss 
    generator:
     ## 将数值类型转换为字符串,解决long型精度丢失
      write_numbers_as_strings: true

方法2:

数据库返回的数据

数据库返回(Java类型)类型小数位默认添加
Double
1.002位
BigDecimal
1.01位

手动对返回的小数,进行转化,转换为字符串,再替换掉结尾的.00或者.0

rowValue = (number instanceof Double && number != null && number.toString().endsWith(".0")) ? rowValue.replace(".0", "") :rowValue;
rowValue = (number instanceof BigDecimal && number != null && number.toString().endsWith(".00")) ? rowValue.replace(".00", "") :rowValue;
rowValue = (number instanceof Double && number != null && number.toString().endsWith(".000")) ? rowValue.replace(".000", "") :rowValue;
             
举报

相关推荐

0 条评论