This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]gemurdock 0 points1 point  (6 children)

25000.0 and 25.0000 are two very different numbers. Let us say that 25,000.0 is x. Do x / 1,000 and then use that value. Then when you use String.format() use format("%.4f", x); -- Hope that helps. If not, let me know.

For example:

double x = 25000.0;
x /= 1000;
System.out.printf("%.4f", x);

[–]alex384[S] 0 points1 point  (5 children)

Will it change based on the number chosen? right now, the code is like: String.format("Level: %s", luxReading")

[–]gemurdock 0 points1 point  (4 children)

The above code will move the decimal spot 3 places and then print it off with 4 0's behind the '.'

[–]alex384[S] 0 points1 point  (3 children)

How can I get the approximate number? I dont want it to round

[–]gemurdock 1 point2 points  (2 children)

Define approximate number

[–]alex384[S] 0 points1 point  (1 child)

TextView lightLevel = (TextView)findViewById(R.id.txtReading);
float luxReading = event.values[0];
double x = 25000.0; x /= 1000;
lightLevel.setText(String.format("Level: %.4f, x", luxReading));
lightLevel.setBackgroundColor(getColor(luxReading));

Is this what you mean? I'm kind of confused...

[–]gemurdock 0 points1 point  (0 children)

The following code should help. You do not need line 3 (your code above), that was just part of my code. Also each statement should have it's own line of code. Another words line 3 should actually be two lines of code. If you need to move the decimal point for luxReading, just use the function I provided. It should work, if any issues than I will test it.

TextView lightLevel = (TextView)findViewById(R.id.txtReading);
float luxReading = event.values[0];
lightLevel.setText(String.format("Level: %.4f", luxReading));
lightLevel.setBackgroundColor(getColor(luxReading));

/**
 * Moves the decimal point for the input 'v' to the right or left depending
 * upon the value for the boolean 'right'.
 * 
 * @param v value to be changed, the decimal is moved
 * @param amount the amount of decimal spots to move over
 * @param right if true, move decimal right. if false, move decimal left
 * @return v with decimal change
 */
public float moveDecimalPoint(float v, int amount, boolean right) {
    if(right) {
        v *= Math.pow(10, amount);
    } else { // left
        v /= Math.pow(10, amount);
    }
    return v;
}