David.m.livingston
David.m.livingston

Reputation: 11

Add parentheses around rounded decimal place

I have to round a number to the third decimal place and display the number like this, 123.12(3). I need some help getting the parentheses around the third decimal place. Here is the code I have:

DecimalFormat reviewRound = new DecimalFormat("##0.000" );

Upvotes: 1

Views: 476

Answers (1)

imtheman
imtheman

Reputation: 4843

I don't think that it is possible just with DecimalFormat. Here is a function I created to get you what you want.

public static String format(double d)
{
    DecimalFormat test = new DecimalFormat("##0.000");
    String str = test.format(d);
    String str2 = str.substring(str.length() - 1);
    str = str.substring(0, str.length() - 1);

    return str + '(' + str2 + ')';
}

Upvotes: 1

Related Questions