Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure rounding to decimal places

Tags:

I have strings which represents the decimal values, ex: "0.010", "0.0100000" "00.01000"

I want to round them to specified format, ex: #.##

In Java we have:

public BigDecimal setScale(int newScale, RoundingMode roundingMode) {     return setScale(newScale, roundingMode.oldMode); } 

What is the best approach to achieve this in Clojure rather than using interop?

like image 891
Isuru Avatar asked May 25 '12 09:05

Isuru


1 Answers

You can use Clojure's format for this purpose. It should provide you a solution to your problem. Here are some examples and a reference:

user=> (format "%.2f" 0.010) "0.01" user=> (format "%.2f" 0.010000) "0.01" user=> (format "%.2f" 00.010000000)   user=> (doc format) ------------------------- clojure.core/format ([fmt & args])   Formats a string using java.lang.String.format, see java.util.Formatter for format   string syntax 
like image 76
number23_cn Avatar answered Sep 28 '22 16:09

number23_cn