简体   繁体   中英

Is there a better way to convert a number to a padded string in Java with a variable decimal precision?

I've searched and cannot find the answer.

Anyway, I need to take a string (which was converted from a number) to another string with the decimal point "assumed". And this decimal precision needs to be variable.

For example, let's say my pseudo method is:

private String getPaddedNumber(String number, Integer decimalPlaces)...

So the user can:

 getPaddedNumber("200", 0);      //   "200"
 getPaddedNumber("200.4", 2);    //   "20040"
 getPaddedNumber("200.4", 1);    //   "2004"
 getPaddedNumber("200.4", 4);    //   "2004000"
 getPaddedNumber("200.", 0);     //   "200"  this is technically incorrect but we may get values like that.

Now, I have actually already programmed a method that does all of this but it's pretty beefy. Then I wondered, "does Java already have a DecimalFormat or something that does this already?

Thanks.

EDIT

These numbers will not be coming in as scientific notation.

Some examples of the numbers:

"55"
"78.9"
"9444.933"

The results would never have a decimal point.

More examples:

getPaddedNumber("98.6", 2);    //  "9860"
getPaddedNumber("42", 0);      //  "42"
getPaddedNumber("556.7", 5);   //  "55670000"

EDIT2

This is the code I am currently using. It's not beautiful but it seems to be working. But I can't help but feel that I've re-invented the wheel. Does Java have something native that does this?

private static String getPaddedNumber(String number, int decimalPlaces) {

    if (number == null) return "";
    if (decimalPlaces < 0) decimalPlaces = 0;

    String working = "";
    boolean hasDecimal = number.contains(".");

    if (hasDecimal) {
        String[] split = number.split("\\.");
        String left = split[0];
        String right;

        if (split.length > 1)
            right = split[1];
        else
            right = "0";

        for (int c = 0; c < decimalPlaces - right.length(); c++)
            working += "0";

        return left + right + working;
    }

    for (int c = 0; c < decimalPlaces; c++)
        working += "0";

    return number + working;
}

You can use BigDecimal class to convert scientific notation to a usable number:

String test = "200.4E2";
int val = new BigDecimal(test).intValue();
double val1 = new BigDecimal(test).doubleValue();
System.out.println("" + val);

etc...

****UPDATE*****

public static void main(String[] args) throws FileNotFoundException {
    String test = "200.4E2";
    String test2 = "200E0";
    String val = new BigDecimal(test).toPlainString();
    String val1 = new BigDecimal(test2).toPlainString();
    System.out.println("" + val);
    System.out.println("" + val1);
}

You can just concatenate your numbers together to get scientific notation:

String test = "200.4" + "E" + 2;

FULL METHOD

private static String getPaddedNumber(String number, int decimalPlaces) {
    String temp = number + "E" + decimalPlaces;
    return new BigDecimal(temp).toPlainString();
}

Code taken from here

类似于number * Math.pow(10, decimalPlaces)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM