rounding off values

324 views
Skip to first unread message

Nikhanj

unread,
Oct 9, 2012, 6:19:29 AM10/9/12
to ne...@googlegroups.com
Hi,
I have a number like this a=0.123456789. I want to round this value to two decimal places. How to do this in gremlin ????

Michael Hunger

unread,
Oct 9, 2012, 6:26:08 AM10/9/12
to ne...@googlegroups.com
it is groovy after all.

(0.452334D).round(2)

Am 09.10.2012 um 12:19 schrieb Nikhanj:

> Hi,
> I have a number like this a=0.123456789. I want to round this value to two decimal places. How to do this in gremlin ????
>
> --
>
>

Nikhanj

unread,
Oct 9, 2012, 6:29:55 AM10/9/12
to ne...@googlegroups.com
I tried but got  No signature of method: java.math.BigDecimal.round() is applicable for argument types: (java.lang.Integer) values: [2] 

Nikhanj

unread,
Oct 9, 2012, 6:31:14 AM10/9/12
to ne...@googlegroups.com
When i try this 0.12345.round(2), i get exception but on doing 0.12345D.round(2) i am able to get the result. Why so???

Michael Hunger

unread,
Oct 9, 2012, 6:46:53 AM10/9/12
to ne...@googlegroups.com
if you append a D it is a double.

for bigdecimals there is 

1.233434.setScale(2,BigDecimal.ROUND_HALF_UP)


--
 
 

Nikhanj

unread,
Oct 9, 2012, 7:05:45 AM10/9/12
to ne...@googlegroups.com
Thanks Michael!!! It works

Marko Rodriguez

unread,
Oct 9, 2012, 10:04:41 AM10/9/12
to ne...@googlegroups.com
Hi,

You can simply use:

Math.round(a);

gremlin> a=0.123456789
==>0.123456789
gremlin> Math.round(a)
==>0

To decide to what decimal you want, you can multiply and divide:

gremlin> Math.round(a * 100) / 100 ==>0.12

Note that Gremlin has full access to the Java and Groovy APIs, so:


HTH,
Marko.

On Oct 9, 2012, at 4:19 AM, Nikhanj wrote:

Hi,
I have a number like this a=0.123456789. I want to round this value to two decimal places. How to do this in gremlin ????

--
 
 

Reply all
Reply to author
Forward
0 new messages