Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Tester {
- public Tester() {
- // TODO Auto-generated constructor stub
- //http://stackoverflow.com/questions/9627182/how-do-i-limit-decimal-precision-in-processing
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println(countFactors(10));
- displayFactors(10);
- float num = 145.378f;
- System.out.println("Decimal Factors2: " + countFactorsD(num,3));
- displayFactors(num,3);
- }
- public static double round2(double num, double places){return Math.round(num * Math.pow( 10.0, places ) ) / Math.pow( 10.0, places );}
- public static int countFactors(int number)
- {
- int count = 0;
- for(int i = 1; i <= number; i++)
- {
- if(number % i == 0)
- {
- count++;
- }
- }
- return count;
- }
- public static int[] getFactors(int number)
- {
- int count[] = new int[countFactors(number)];
- int arrayslot = 0;
- for(int i = 1; i <= number; i++)
- {
- if(number % i == 0)
- {
- count[arrayslot] = i;
- arrayslot++;
- }
- }
- return count;
- }
- public static void displayFactors(int number)
- {
- for(int i = 0; i < getFactors(number).length; i++)
- {
- System.out.print(getFactors(number)[i]+ " ");
- }
- System.out.println();
- }
- public static double generateDCounterNS(int places)
- {
- double d = 1.0;
- for(int i = 0; i <= places; i++)
- {
- if(i < places)
- {
- d /= 10;
- }
- else
- {
- //d /= 10;
- }
- }
- return d;
- }
- public static int countFactorsD(float number, int places)
- {
- int count = 0;
- for(float i = (float)round2(generateDCounterNS(places),places); i <= (float)round2(number,places) + (float)round2(generateDCounterNS(places),places); i += (float)round2(generateDCounterNS(places),places))
- {
- if((float)round2(number,places) % (float)round2(i,places) == (float)round2(0.0,places))
- {
- count++;
- }
- //System.out.println("Counter: " + (float)round2(i,1) + " What is " + number + " % " + i + " = " + (float)round2(number % (float)round2(i,1),1) );
- }
- return count;
- }
- public static float[] getFactors(float number, int places)
- {
- float count[] = new float[countFactorsD(number, places)];
- int arrayslot = 0;
- for(float i = (float)round2(generateDCounterNS(places),places); i <= (float)round2(number,places) + (float)round2(generateDCounterNS(places),places); i += (float)round2(generateDCounterNS(places),places))
- {
- if((float)round2(number,places) % (float)round2(i,places) == (float)round2(0.0,places))
- {
- count[arrayslot] = (float)round2(i,places);
- arrayslot++;
- }
- }
- return count;
- }
- public static void displayFactors(float number, int places)
- {
- float[] numbers = getFactors(number, places);
- for(int i = 0; i < numbers.length; i++)
- {
- System.out.print(numbers[i]+ " ");
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement