在flink计算过程中,数据量一大,就会出现科学计数法的表示方式,但有时候我们又要避免科学计数法的展示,一下是我的一个小例子
var total:Double = 0
row_stream.process( new ProcessFunction[Row, Double] {
override def processElement(i: Row, context: ProcessFunction[Row, Double]#Context, collector: Collector[Double]): Unit = {
total = total + (i.getFieldAs[Int]("product_cnt") * i.getFieldAs[Double]("product_price"))
collector.collect(total)
}
} ).addSink(new RedisSink[Double](conf, new MyRedis))
上述代码写入redis中并用redis-cli查询时会出现科学计数法
127.0.0.1:6379> get totalprice
"5.015750414999975E7"
我们可以用到java.text.DecimalFormat
import java.text.DecimalFormat
object Main {
def main(args: Array[String]): Unit = {
val decimalFormat = new DecimalFormat("#.##") // 指定格式
val value = 123456789.123456789
val resValue = decimalFormat.format(value) // 格式化
}
}
其中,"#.##" 表示保留两位小数,该方法的更多详细内容可以看这篇Java 详解数字格式化(NumberFormat&DecimalFormat)-CSDN博客
把该方法运用到上述代码中,问题解决
127.0.0.1:6379> get totalprice
"51685600.87"