xodhx4
5/17/2018 - 3:57 PM

float formatting

scala formating the float

def plusMinus(arr: Array[Int]) {
    val total: Double = arr.length
    val pos = arr.filter((i: Int) => i > 0).length / total
    val neg = arr.filter((i: Int) => i < 0).length / total
    val zero = arr.filter((i: Int) => i == 0).length / total
    println(f"$pos%1.6f")
    println(f"$neg%1.6f")
    println(f"$zero%1.6f")
}
/*
0.500000
0.333333
0.166667
*/