Skip to main content
Announcements
SYSTEM MAINTENANCE: Thurs., Sept. 19, 1 AM ET, Platform will be unavailable for approx. 60 minutes.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[resolved] How to make float round to 2 decimal places in tMap?

Hi, I tried setting length to 5 and precision to 2 (just guessing), but that didn't affect my output number at all
I'm getting 6 decimal places now with no length or precision settings
Thanks 0683p000009MACn.png
Labels (2)
1 Solution

Accepted Solutions
_AnonymousUser
Specialist III
Specialist III

Try this one..... round a number to 2 decimal places
Mercal 

View solution in original post

4 Replies
Anonymous
Not applicable
Author

Hello, ensure that your columns are BigDecimal types first. Then on the far side of a tMap or other component use something like:
input_row.column.scale(2,BigDecimal.ROUND_HALF_UP)

As an example, I put the following into a tJava to test it and got the results you're looking for:
BigDecimal bd = new BigDecimal(2.437732);
BigDecimal bd2 = bd.setScale(2,BigDecimal.ROUND_HALF_UP);
System.out.println("bd2 = "+bd2);

and it outputs:
Starting job test at 17:13 22/05/2014.
connecting to socket on port 3511
connected
bd2 = 2.44
disconnected
Job test ended at 17:13 22/05/2014.
Anonymous
Not applicable
Author

Thanks for that, this is a handy reference
For this Job...
... While originally testing with a CSV file, I have found that MySQL dealt with adequately it by making it a 'decimal' db data type and limiting the length to "5,2"
I will mark the thread "resolved" ? would mark it "double resolved" if I could! 0683p000009MACn.png
_AnonymousUser
Specialist III
Specialist III

Try this one..... round a number to 2 decimal places
Mercal 
RMotta2408
Creator II
Creator II

If they are floats, you don't need to turn them into bigdecimals.

You could easily use Math.round().

I'll explain:

Var1 is type float.

Var1 = 2,539

Math.round(Var1) is 3

Var1 * 100 is 253,9

This means that Math.round(Var1 * 100) is 254

This also means that Math.round(Var1 * 100) / 100 is 2,54

So, if you want to round a float to 2 decimal places, do this:

  • Var2 = Math.round(Var1 * 100) / 100

Var2 = 2,54