[英]Difficulty parsing a double - String
Thanks for taking the time to answer my question. 感谢您抽出宝贵时间回答我的问题。
I have a double
value (let's say 22.35). 我有一个double
值(比方说22.35)。 I need to parse it into a String
and get 2235. The following code is not working properly. 我需要将其解析为String
并获取2235.以下代码无法正常工作。
double totalPrice = 22.35;
DecimalFormat df = new DecimalFormat("#.##");
String[] temp = df.format(totalPrice).split(".");
String amount = temp[0] + temp[1];
I keep getting an exception ArrayIndexOutOfBounds
. 我一直得到一个异常ArrayIndexOutOfBounds
。 What is another way to do this? 另一种方法是什么? Thanks in advance! 提前致谢!
If your values don't exceed, after multiplication by 100, MAX_INT, multiply them: 如果您的值不超过,则在乘以100后,MAX_INT将它们相乘:
double totalPrice = 22.35;
int iPrice = (int) (totalPrice * 100);
String sPrice = "" + iPrice;
What about: 关于什么:
double totalPrice = 22.35;
DecimalFormat df = new DecimalFormat("#.##");
String price = df.format(totalPrice).replace(".", "");
Maybe you can do it like this: just change the regex "." 也许你可以这样做:只需改变正则表达式“。” to " \\."! 至 ” \\。”!
String[] temp = df.format(totalPrice).split("\\.");
This will work no matter how high a number you try to apply it to: 无论你尝试将它应用到多高的数字,这都会有效:
double num = 22.35;
String concatenate = "" + num;
String parsedNum = concatenate.replace(".", "");
Hope this helps you. 希望这对你有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.