Error with conversion of units

Asked

Viewed 51 times

0

Can someone help me in what is going wrong in this code?

public class Conversor {
    public static String getUnidades(long quantidade) {
        return getUnidades((float) quantidade);
    }

    public static String getUnidades(int quantidade) {
        return getUnidades((float) quantidade);
    }

    public static String getUnidades(float quantidade) {
        quantidade = quantidade / 1024 / 1024;
        return  quantidade>=1024? (quantidade>=1024*1024?(("%.2f", quantidade /1024/1024) + "TB"):(("%.2f", quantidade/1024) + "GB")):(("%.2f", quantidade) + "MB");

    }
}

It is a method to convert units!

  • An example of output you use and the expected result vs the obtained result helps.

  • I will edit, thanks for the tip It is a method to convert units!

1 answer

1


String.format() is missing before each "amount".

By the way, excellent use of if in Return. It became a very dynamic code.

return  quantidade>=1024? (quantidade>=1024*1024?( String.format("%.2f", quantidade /1024/1024) + "TB"):(String.format("%.2f", quantidade/1024) + "GB")):(String.format("%.2f", quantidade) + "MB");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.