医院网站建设 价格低,深圳市高端网站建设,wordpress手机号登陆不了,广西电力工程建设公司网站1、背景
在使用spring boot时#xff0c;前端的界面展示的数据是2 #xff0c;在数据库中存储的是小数。但是导出Excel的时候数据是 2.00 。奇了怪了为啥会不一样#xff0c;数据都是一样的没有做过处理。 2、排查问题
经过层层的debug 发现数据库返回的数据是2.00#x…1、背景
在使用spring boot时前端的界面展示的数据是2 在数据库中存储的是小数。但是导出Excel的时候数据是 2.00 。奇了怪了为啥会不一样数据都是一样的没有做过处理。 2、排查问题
经过层层的debug 发现数据库返回的数据是2.00写入Excel的数据就是查询数据库原始的数据。
ListMapString, 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;