First of all, hello Im Joey and Ive recently started messing around with the Java language. Ill do my best not to bother you alot with my newbie questions :)

So I need to create a method that returns some variables I have encapsulated, as String but without the last digit.
So :

double value1 = 23.45
double value2 = 11.47

The output should be

value 1 is 23.4
value 2 is 11.5

Ive got this far :)

public String toString(){		
     return ("X = " + Double.toString(value1) + " , Y = " + Double.toString(value2));
}

Any help would be great

Recommended Answers

All 3 Replies

One question from my side: Do you want to save 1 digit after the comma, or you you want to remove the last digit of the number?

Assuming you want to remove one digit:
- Make a string out of the double, adding
- Copy the string, but without the last character
- Copy another string, with only the last character of the first string.
- Check if the value of the single last digit is 5 or higher, and set a boolean
- If the boolean is false, use the value of the string of the second step.
- Otherwise, use the value string, and add 1 to the last digit of the double.
- Print out the generated number.

There are other options ofcourse, but this is one I have used once, it worked.

Member Avatar for hfx642

Well... You could do...

double value1r = (Math.round (value1 * 10)) / 10.0;
double value2r = (Math.round (value2 * 10)) / 10.0;

Thank you I got it now. Cheers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.