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

5 Years
Discussion Span
Last Post by Joey_Brown

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.


Well... You could do...

double value1r = (Math.round (value1 * 10)) / 10.0;
double value2r = (Math.round (value2 * 10)) / 10.0;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.