Divide String by String [] by period, but return an empty array

Well, maybe I just need a second pair of eyes.

I have a float, I turn into a string. Then I want to break it down into a period / decimal to represent it as a currency.

Here is 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 error outside the bounds of line 6 (in the code above) that there is nothing left after the decimal value.

Actually on line 5, when I print the size of the array, it is 0.

These are ridiculous mistakes for them to NOT be what I just ignore.

As I said, another pair of eyes is exactly what I need, so please do not be rude, pointing out what is obvious to you, but I did not pay attention to it.

Thanks in advance!

+6
source share
5 answers

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

 "\\." 

EDIT: fixed, thanks to the commentator and editor

+19
source

Use decimal format instead:

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

I did not work much on java, but in line 2, perhaps the price is not converted to a string. I am working in C # and I would use this as: String itemsPrice = "" + price.ToString ();

Perhaps you need to convert the price to a string first. Since it cannot be converted, the string contains only ", and no." ", Therefore there is no expansion or extension of arrayOutOfBounds.

0
source

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.

In addition, it is usually a bad idea to present prices in an inaccurate format (for example, floating point). You must use BigDecimal or Decimal.

0
source

If you want to process everything yourself, 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 truncated price like this:

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

Source: https://habr.com/ru/post/914774/


All Articles