rounding off

Page 1 of 1

4 Replies - 9573 Views - Last Post: 11 May 2007 - 12:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=27832&amp;s=e15500d7c24a90325809e94e7bde6079&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 gyron

Reputation: 0
• Posts: 76
• Joined: 09-January 07

rounding off

Posted 09 May 2007 - 05:07 AM

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)
Is This A Good Question/Topic? 0

Replies To: rounding off

#2 Ellie

Reputation: 8
• Posts: 533
• Joined: 17-January 07

Re: rounding off

Posted 09 May 2007 - 09:05 AM

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

```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 phantom2850

Reputation: 0
• Posts: 8
• Joined: 09-May 07

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.

```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 gyron

Reputation: 0
• Posts: 76
• Joined: 09-January 07

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.

#5 phantom2850

Reputation: 0
• Posts: 8
• Joined: 09-May 07

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.

Quote

phantom2850:
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..