0

My requirement is that if the last variable value is less than 1 for example 0.0045

then i need to print 4 digits after the decimals so that the result will look like 0.0045

or else if the last variable value is greater than 1 for example 444.8183

then i need to print only 2 digits after the decimals so that the result will look like 444.82

I have written the program , its working fine , but i like to use the ternary opearator

public class Test {

    private static NumberUtil numberUtil = NumberUtil.getInstance();

    public static void main(String args[]) {
        float last = (float) 444.8183;
        String result = "";

        if (last > 1) {
                result = numberUtil.formatNumber(last, 2);
        } else {
            result = numberUtil.formatNumber(last, 4);
        }
        System.out.println(result);
    }
}

import java.text.DecimalFormat;

public class NumberUtil {

    private static NumberUtil _instance = new NumberUtil();

    public static NumberUtil getInstance() {
        return _instance;
    }

    public String formatNumber(double d, int decPts) {
        if (2 == decPts)
            return new DecimalFormat("#,###,###,##0.00").format(d);
        else if (0 == decPts)
            return new DecimalFormat("#,###,###,##0").format(d);
        else if (3 == decPts)
            return new DecimalFormat("#,###,###,##0.000").format(d);
        else if (4 == decPts)
            return new DecimalFormat("0.0000").format(d);
        return String.valueOf(d);
    }

    public double formatDoubleNumber(double d){
        double newD = Math.round(d*100.0)/100.0;
        return newD;
    }
}
mishik
  • 9,973
  • 9
  • 45
  • 67
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • 1
    Possible duplicate of [How do I use the conditional operator?](http://stackoverflow.com/questions/392932/how-do-i-use-the-conditional-operator) – 10 Replies Feb 04 '16 at 00:39
  • There are so many __massive__ spelling/grammar mistakes and the question is one that could be answered very easily with google. You do not need internet minions to program this for you. – 10 Replies Feb 04 '16 at 00:41

5 Answers5

8

How about

sigFigs = (last > 1)? 2 : 4;
result = numberUtil.formatNumber(last, sigFigs);

The advantage of splitting it up like this is that the code becomes "self documenting". Next week you will still remember why you did what you did.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • 1
    +1 don't try to cram too much into one line. Code should be as clear as possible before it is made as short as possible. – David Jun 10 '13 at 04:04
3

To make it more readable, put the ternary inside the call:

String result = numberUtil.formatNumber(last, last > 1 ? 2 : 4);

The important thing here is you're narrowing down the effect of ternary to the minimum scope, in line with "minimising scope".

Note that you don't need to use brackets around the test (and less code is good!).

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Dear downvoter, what is not useful about this answer??? I would use this code in production no problem. – Bohemian Jun 10 '13 at 04:58
  • See the answer by Floris (currently the top answer). Your code is unclear by comparison... which is typically of code that uses the conditional operator. Readability is worth more than saving a line. – TofuBeer Jun 10 '13 at 21:52
  • @tofu I fully agree about readability being more important than code density. For me, this is clear enough, but the other answer is more clear because the ternary result is named (by the variable name). I really just answered this ax an alternative. – Bohemian Jun 10 '13 at 21:55
2

Here's how:

result = (last > 1) 
    ? numberUtil.formatNumber(last, 2) 
    : numberUtil.formatNumber(last, 4);

By the way, it's actually called the "conditional operator". It happens to be "a" ternary operator (ternary means that it takes three operands).

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
0

I dont like the ternary operator but here you go

result =  (last > 1)? numberUtil.formatNumber(last, 2) : numberUtil.formatNumber(last, 4);
BevynQ
  • 8,089
  • 4
  • 25
  • 37
0

result = numberUtil.formatNumber(last, (last > 1) ? 2 : 4);

Elazar
  • 20,415
  • 4
  • 46
  • 67