Reputation: 3
Instead of using this approach:
a.map(100 * _.answer.toFloat).map(_.toInt + "%").getOrElse("")
How can I apply math.round approach in above code.
Thanks
Upvotes: 0
Views: 129
Reputation: 49705
Arguably a comment, but I needed to include code...
Jesper's answer can be improved by collapsing the double-map, and extracting the method to calculate a percent string:
def percentStr(x: Float) = (100*x).round.toString + "%"
a.map(x => percentStr(x.answer.toFloat)).getOrElse("")
Upvotes: 1
Reputation: 206796
What exactly do you mean, you want to round instead of use toInt
, which just cuts off the decimals? Then just use round
instead of toInt
:
a.map(100 * _.answer.toFloat).map(_.round + "%").getOrElse("")
Upvotes: 2