Java 计算 2022 年每个月的环比增长率
假设 2022 年 1 月到 12 月的数据分别存储在一个数组中,可以使用以下代码计算每个月的环比:
double[] data2022 = {100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
double[] data2021 = {90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200};
for (int i = 0; i < data2022.length; i++) {
double mom = (data2022[i] - data2021[i]) / data2021[i];
System.out.printf("2022年%d月环比为:%.2f%%
", i+1, mom*100);
}
其中,使用 for 循环遍历每个月的数据,计算每个月的环比,并使用 printf 方法输出结果。注意,环比需要乘以 100 转换为百分比,并使用 %.2f 格式化输出结果,保留两位小数。
原文地址: https://www.cveoy.top/t/topic/mMOR 著作权归作者所有。请勿转载和采集!