rounding off

round of to three 'working digits'

Page 1 of 1

4 Replies - 7833 Views - Last Post: 11 May 2007 - 12:54 PM Rate Topic: -----

#1 gyron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • 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  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 8
  • View blog
  • 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 :D
Was This Post Helpful? 0
  • +
  • -

#3 phantom2850  Icon User is offline

  • New D.I.C Head

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

Re: rounding off

Posted 09 May 2007 - 11:27 AM

View PostEllie, 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 :D


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

Was This Post Helpful? 0
  • +
  • -

#4 gyron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 phantom2850  Icon User is offline

  • New D.I.C Head

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

Re: rounding off

Posted 11 May 2007 - 12:54 PM

View Postgyron, 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..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1