I need to develop a function that rounds off numbers to '3 working digits', i.e. if a number is:

1,220 it gets trimmed to 1000

1,970 gets to be 2000

203,420,120 gets to 203,000,000

Is there an inbuilt function that does that, or i need to develop my own? (no need for reinventing the wheel)

## 4 Replies - 9188 Views - Last Post: 11 May 2007 - 12:54 PM

##
**Replies To:** rounding off

### #2

## Re: rounding off

Posted 09 May 2007 - 09:05 AM

OK, here is method for rounding a 4 digit number as your first 2.

To do the same with larger numbers, then your programme needs to detect the length of number, and divide by the right amount. Math.round() works to round off any decimals so if you force your number to have all the unwanted digits after the decimal point, they are rounded by Math.round(). If you want to just discard the last digits rather than round them (which could obviously go up too) then use Math.floor().

Hope that helps

import java.io.*; public class Round { public static void main(String[] args) { int s = 1220; int rounded = Math.round(s/1000); rounded = rounded*1000; System.out.println("rounded: " + rounded); System.exit(0); }

To do the same with larger numbers, then your programme needs to detect the length of number, and divide by the right amount. Math.round() works to round off any decimals so if you force your number to have all the unwanted digits after the decimal point, they are rounded by Math.round(). If you want to just discard the last digits rather than round them (which could obviously go up too) then use Math.floor().

Hope that helps

### #3

## Re: rounding off

Posted 09 May 2007 - 11:27 AM

Ellie, on 9 May, 2007 - 09:05 AM, said:

OK, here is method for rounding a 4 digit number as your first 2.

To do the same with larger numbers, then your programme needs to detect the length of number, and divide by the right amount. Math.round() works to round off any decimals so if you force your number to have all the unwanted digits after the decimal point, they are rounded by Math.round(). If you want to just discard the last digits rather than round them (which could obviously go up too) then use Math.floor().

Hope that helps

import java.io.*; public class Round { public static void main(String[] args) { int s = 1220; int rounded = Math.round(s/1000); rounded = rounded*1000; System.out.println("rounded: " + rounded); System.exit(0); }

To do the same with larger numbers, then your programme needs to detect the length of number, and divide by the right amount. Math.round() works to round off any decimals so if you force your number to have all the unwanted digits after the decimal point, they are rounded by Math.round(). If you want to just discard the last digits rather than round them (which could obviously go up too) then use Math.floor().

Hope that helps

My friend Ellie, That is not what gyron is looking for...

Gyron, there is no inbuilt method in java to do what you asked for, You have to write a small code for it...

I suppose this will serve your purpose...

public class Main { public static void main(String[] args) { int s = 1223450; int len = 0; len = new Integer(s).toString().length(); len = (int)Math.pow(10,(len - 3)); s = Math.round(s/len)*len; } }

I am presuming that you wont be using very large value, hence i have cast pow() return from long to int...

Warm Regards,

Chetan.

This post has been edited by **phantom2850**: 09 May 2007 - 11:37 AM

### #4

## Re: rounding off

Posted 10 May 2007 - 06:35 AM

Yeah, this is what i wanted. We use very big numbers here. This is actually how we round off the selling price of an item! Our currency lost it's value and keeps on depreciating. It's so bad we had to discard 3 zeros to make trillions billions.

Thanks guys.

Thanks guys.

### #5

## Re: rounding off

Posted 11 May 2007 - 12:54 PM

gyron, on 10 May, 2007 - 06:35 AM, said:

Yeah, this is what i wanted. We use very big numbers here. This is actually how we round off the selling price of an item! Our currency lost it's value and keeps on depreciating. It's so bad we had to discard 3 zeros to make trillions billions.

Thanks guys.

Thanks guys.

Quote

phantom2850:

len = (int)Math.pow(10,(len - 3));

s = Math.round(s/len)*len;

len = (int)Math.pow(10,(len - 3));

s = Math.round(s/len)*len;

if you are using large value then, Declare a long variable : long result =0;

Then store the value of Math.pow(10,(len - 3)) in it and remove int casting done. and continue..

Page 1 of 1