简体   繁体   中英

Split String to String[] by a period but returning an empty array

Ok maybe i just need a second pair of eyes on this.

I have a float, that I turn into a string. I then want to split it by its period/decimal in order to present it as a currency.

Heres my code:

float price = new Float("3.76545");
String itemsPrice = "" + price;
if (itemsPrice.contains(".")){
    String[] breakByDecimal = itemsPrice.split(".");
    System.out.println(itemsPrice + "||" + breakByDecimal.length);
    if (breakByDecimal[1].length() > 2){
        itemsPrice = breakByDecimal[0] + "." + breakByDecimal[1].substring(0, 2);
    } else if (breakByDecimal[1].length() == 1){
        itemsPrice = breakByDecimal[0] + "." + breakByDecimal[1] + "0";                         
    }                           
}

If you take this and run it, you will get an array index out of bounds error on line 6 (in the code above) regarding there being nothing after a decimal.

In fact on line 5, when i print out the size of the array, it's 0.

These are to ridiculous of errors for them to NOT be something i am simply overlooking.

Like I said, another pair of eyes is exactly what i need, so please don't be rude when pointing out something that's obvious to you but I overlooked it.

Thanks in advance!

split uses regular expressions, in which "." means match any character. you need to do

"\\."

EDIT: fixed, thanks commenter&editor

Use decimal format instead:

DecimalFormat formater = new DecimalFormat("#.##");
System.out.println(formater.format(new Float("3.76545")));

I have not worked much on java, but on line 2, maybe price is not getting converted to string. I work in C# and I would use that as : String itemsPrice = "" + price.ToString();

maybe you should convert price to string first explicitly. Since, it is not getting converted, string only contains "" and there is no ".", so no split and by extension arrayOutOfBounds error.

If you want to present it as a price use NumberFormat.

Float price = 3.76545;
Currency currency = Currency.getInstance(YOUR_CURRENCY_STRING);
NumberFormat numFormat = NumberFormat.getCurrencyInstance();
numFormat.setCurrency(currency)
numFormat.setMaximumFractionDigits(currency.getDefaultFractionDigits());
String priceFormatted = numFormat.format(price);
System.out.println("The price is: " + priceFormatted);

YOUR_CURRENCY_STRING is the ISO 4217 currency code for the currency you are dealing with.

Also, it's generally a bad idea to represent prices in a non-precise format (such as floating point). You should use BigDecimal or Decimal.

If you want to handle it all by yourself then try the following code:

public static float truncate(float n, int decimalDigits) {
    float multiplier = (float)Math.pow(10.0,decimalDigits);
    int intp = (int)(n*multiplier);
    return (float)(intp/multiplier);
}

and get the truncatedPrice like this:

float truncatedPrice = truncate(3.3654f,2);
System.out.println("Truncated to 2 digits : " + truncatedPrice);

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