0

Hi guys I need some help with calculating and displaying a ratio result.

so here is the sample code I am working on:

    double a = 11;
    double b = 2508;
    double total1;
    double total2;

    total1 = a / b;

    System.out.println(total1);

Now the result I get is 0.0043859649122807015 . However I only need the "0.004" the three decimal places, and to calculate the ratio I will need to round "0.004" in to 1 so how do I do that?

Phase2

    double a = 11;
    double b = 2508;
    double total1;
    double total2;

    total1 = a / b;

    System.out.println(total1);

    total2 = 1/total1; //1 is equal a rounded 0.004 then devided by 0.004

    System.out.println(total2);

But I had to manually put 1 to do the calculation. is there a way to store total 1 from the first calculation? total1 = a/b; . So I can call it back to do a "total2 = "A rounded 1 of 0.004"/total1;

Finally print the result out and is 250.

Little formula

11/2508 = 0.004

0.004 = 1 

1/0.004= 250

1:250

I have tried to use decimalformat on j2me but it does not support it

Ket
  • 273
  • 2
  • 8
  • 20

2 Answers2

1

You need a number formatter

http://docs.oracle.com/javase/tutorial/java/data/numberformat.html

System.out.format("%.3f%n", pi);     // -->  "3.142"

Also just flipwhat your formula.

2508/11 = 228

so 1:228 which is more accurate than 1:250

exussum
  • 18,275
  • 8
  • 32
  • 65
  • 1
    J2ME doesn't support formatter, is there anyway go around it? – Ket Dec 18 '12 at 14:20
  • String[] patterns = new String[] { "#,#00.00#", "0.0;(0.0)", "0.###E0" }; DecimalFormat format = (DecimalFormat) DecimalFormat .getNumberInstance(); double value = -12.321; for (int i = 0; i < patterns.length; i++) { String pattern = patterns[i]; format.applyPattern(pattern); String text = "Pattern: " + pattern + " Sample: "+format.format(value)+"\n"; add(new LabelField(text)); } from http://stackoverflow.com/questions/2094700/j2me-number-format – exussum Dec 18 '12 at 14:22
  • But im not sure why you are losing accuracy when you can do 2508/11 to give the ratio direct ? – exussum Dec 18 '12 at 14:23
1

You should use Math.round() and Math.log10().

log10 determines the location of decimal point, having log10=0 for 1.

So, first you calculate decimal point location:

Math.log10(total1);

this will give

-2.357934847000454

round it with Math.round() which will give

-2

this is the number you use in calculations.

Final code is

    double a = 11;
    double b = 2508;
    double total1;
    double total2;

    total1 = a / b;

    System.out.println(total1);

    // determining point location
    long l = Math.round(Math.log10(total1));

    System.out.println(l);

    // moving point right
    total1 = total1 * Math.pow(10, -l+1);

    System.out.println(total1);

    // rounding
    total1 = Math.round(total1);

    System.out.println(total1);

    // moving point back
    total1 = total1 / Math.pow(10, -l+1);

    System.out.println(total1);

    total2 = 1/total1; //1 is equal a rounded 0.004 then devided by 0.004

    System.out.println(total2);

P.S.

Also you can learn something from this: How to convert floats to human-readable fractions?

Community
  • 1
  • 1
Dims
  • 47,675
  • 117
  • 331
  • 600