Java 使用 int 数据计算百分比

int diliverNum=3;//举例子的变量
int queryMailNum=9;//举例子的变量
// 创建一个数值格式化对象   
NumberFormat numberFormat = NumberFormat.getInstance();   
// 设置精确到小数点后2位   
numberFormat.setMaximumFractionDigits(2);   
String result = numberFormat.format((float)diliverNum/(float)queryMailNum*100);
System.out.println("diliverNum和queryMailNum的百分比为:" + result + "%");  

 

好的,您可以按照以下步骤来实现 mybatis-plus 分组计算百分比: 1. 在 Mapper 接口中定义对应的 SQL 语句,使用 GROUP BY 子句进行分组,并统计每组的总数。 ```java @Select("SELECT type, COUNT(*) AS total FROM my_table GROUP BY type") List<Map<String, Object>> countByType(); ``` 2. 在实体类中添加计算百分比的属性,并在对应的 getter 方法中进行计算。 ```java public class MyEntity { private String type; private int total; private double percentage; // Getter and Setter ... public void setPercentage(double percentage) { this.percentage = percentage; } public double getPercentage() { return ((double) total / (double) totalCount) * 100.0; } } ``` 3. 在 Service 类中调用 Mapper 接口的方法获取统计数据,并将数据封装到实体类中。 ```java @Service public class MyService { @Autowired private MyMapper myMapper; public List<MyEntity> countPercentageByType() { List<Map<String, Object>> countList = myMapper.countByType(); int totalCount = countList.stream().mapToInt(map -> Integer.parseInt(map.get("total").toString())).sum(); List<MyEntity> resultList = new ArrayList<>(); for (Map<String, Object> countMap : countList) { MyEntity entity = new MyEntity(); entity.setType(countMap.get("type").toString()); entity.setTotal(Integer.parseInt(countMap.get("total").toString())); entity.setTotalCount(totalCount); resultList.add(entity); } return resultList; } } ``` 4. 在 Controller 类中调用 Service 类的方法获取统计数据,并将数据返回给前端展示。 ```java @RestController public class MyController { @Autowired private MyService myService; @GetMapping("/countPercentage") public List<MyEntity> countPercentage() { return myService.countPercentageByType(); } } ``` 以上就是使用 mybatis-plus 分组计算百分比的简单实现方式。需要注意的是,如果统计数据量较大,建议使用数据库的分页查询功能,避免一次性查询过多数据导致性能问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值