简体   繁体   English

将String拆分为String []一段时间但返回一个空数组

[英]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. 如果你拿这个并运行它,你将在第6行(在上面的代码中)得到一个数组索引越界错误,关于小数后面没有任何内容。

In fact on line 5, when i print out the size of the array, it's 0. 实际上在第5行,当我打印出数组的大小时,它为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 "." split使用正则表达式,其中“。” 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. 我没有在java上工作太多,但在第2行,也许价格没有转换为字符串。 I work in C# and I would use that as : String itemsPrice = "" + price.ToString(); 我在C#中工作,我会将其用作: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. 因为,它没有被转换,字符串只包含“”而没有“。”,所以没有拆分和扩展arrayOutOfBounds错误。

If you want to present it as a price use NumberFormat. 如果您想将其作为价格使用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. YOUR_CURRENCY_STRING是您正在处理的货币的ISO 4217货币代码。

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. 你应该使用BigDecimal或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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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