簡體   English   中英

難以解析雙字符串

[英]Difficulty parsing a double - String

感謝您抽出寶貴時間回答我的問題。

我有一個double值(比方說22.35)。 我需要將其解析為String並獲取2235.以下代碼無法正常工作。

double totalPrice = 22.35;
DecimalFormat df = new DecimalFormat("#.##");
String[] temp = df.format(totalPrice).split(".");
String amount = temp[0] + temp[1];

我一直得到一個異常ArrayIndexOutOfBounds 另一種方法是什么? 提前致謝!

如果您的值不超過,則在乘以100后,MAX_INT將它們相乘:

double totalPrice = 22.35;
int iPrice = (int) (totalPrice * 100);
String sPrice = "" + iPrice;

關於什么:

double totalPrice = 22.35;
DecimalFormat df = new DecimalFormat("#.##");
String price = df.format(totalPrice).replace(".", "");

也許你可以這樣做:只需改變正則表達式“。” 至 ” \\。”!

String[] temp = df.format(totalPrice).split("\\.");

無論你嘗試將它應用到多高的數字,這都會有效:

double num = 22.35;
String concatenate = "" + num;
String parsedNum = concatenate.replace(".", "");

希望這對你有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM