Use DecimalFormat to get varying amount of decimal places
Asked Answered
G

4

6

So I want to use the Decimal Format class to round numbers:

double value = 10.555;

DecimalFormat fmt = new DecimalFormat ("0.##");

System.out.println(fmt.format(value));

Here, the variable value would be rounded to 2 decimal places, because there are two #s. However, I want to round value to an unknown amount of decimal places, indicated by a separate integer called numPlaces. Is there a way I could accomplish this by using the Decimal Formatter?

e.g. If numPlaces = 3 and value = 10.555, value needs to be rounded to 3 decimal places

Gannet answered 10/2, 2014 at 1:58 Comment(7)
Are you committed to using DecimalFormat? Because it's much easier if you use Formatter instead.Retrograde
Why would you unaccept my answer and accept his? He copied my answer after I posted mine...Teets
? You edited your answerGannet
@RealMadrid_CF I just edited my answer to make it specifically what you preferred. However, I posted my answer 5 minutes before he posted his and then he copied what I wrote. He even used the same example number 5. You already accepted mine, so I don't see why you would unaccept it. Mine's more well-written anyways and was first.Teets
Your first post was not the same as Don's. You saw that I marked his answer with a check, so you changed your answer to match his. Look at your edit history.Gannet
Ok if it makes you happy then I will mark your answer as correct...Gannet
@RealMadrid_CF Thank you. And no, his answer is the same as mine. I added in the last part afterwards, but he copied my way of solving the problems (using a for loop to add #s). The only difference between his answer and mine originally was he took my code out of the method.Teets
T
7

Create a method to generate a certain number of # to a string, like so:

public static String generateNumberSigns(int n) {

    String s = "";
    for (int i = 0; i < n; i++) {
        s += "#";
    }
    return s;
}

And then use that method to generate a string to pass to the DecimalFormat class:

double value = 1234.567890;
int numPlaces = 5;

String numberSigns = generateNumberSigns(numPlaces);
DecimalFormat fmt = new DecimalFormat ("0." + numberSigns);

System.out.println(fmt.format(value));

OR simply do it all at once without a method:

double value = 1234.567890;
int numPlaces = 5;

String numberSigns = "";
for (int i = 0; i < numPlaces; i++) {
    numberSigns += "#";
}

DecimalFormat fmt = new DecimalFormat ("0." + numberSigns);

System.out.println(fmt.format(value));
Teets answered 10/2, 2014 at 2:5 Comment(2)
Is it possible that I just add the DecimalFormat fmt = new DecimalFormat ("0." + numberSigns);, and not have to add the String method?Gannet
@RealMadrid_CF: Sure it is. You can move the code from the method into wherever you are using the DecimalFormat. The method is just a way of generating a certain number of #, but you can do it another way if you wanted. However, a method is good practise and it can be reused in multiple parts of code, classes, etc.Teets
A
5

If you don't need the DecimalFormat for any other purpose, a simpler solution is to use String.format or PrintStream.format and generate the format string in a similar manner to Mike Yaworski's solution.

int precision = 4; // example
String formatString = "%." + precision + "f";
double value = 7.45834975; // example
System.out.format(formatString, value); // output = 7.4583
Avruch answered 10/2, 2014 at 2:14 Comment(1)
You saved my day, Thank you, before this was using RoundingMode.CEILING in kotlin and decimalFormat which was working good but sometimes it returns ex: 7123,12 and i want 7123.12 by using this i got the solution.Minuend
L
2

How about this?

double value = 10.5555123412341;
int numPlaces = 5;
String format = "0.";

for (int i = 0; i < numPlaces; i++){
    format+="#";
}
DecimalFormat fmt = new DecimalFormat(format);

System.out.println(fmt.format(value));
Lohrman answered 10/2, 2014 at 2:8 Comment(2)
Whatever the case is, you obviously read mine before posting yours (you used the same number of decimal places as me). I don't think you directly copied me; all I was trying to get across was that I posted first and with a more full solution. He was claiming I copied you, so I turned that around. I've upvoted your answer now that this is solved. I've also taken out the message to me in your post. Thanks and sorry.Teets
actually, I didn't had the chance to read yours. right after posting, stack overflow said someone posted a minute before me. But I still posted. And since the number in the example is mostly composed of 5's, I decided to make the decimal places 5, also.Lohrman
O
-2

If you're not absolutely bound to use DecimalFormat, you could use BigDecimal.round() for this in conjunction with MathContext of the precision that you want, then just BigDecimal.toString().

Ollie answered 10/2, 2014 at 3:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.