久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

DecimalFormat 可變組大小

DecimalFormat variable group size(DecimalFormat 可變組大小)
本文介紹了DecimalFormat 可變組大小的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

在發布問題之前,我已經對該主題進行了一些研究,但找不到答案.

I've researched the subject somewhat before posting the question, but I couldn't find the answer.

這是我想要做的:

輸入:一個長 7-8 個小數位的數字(無分數).

input: a number 7-8 decimal spaces long (no fractions).

輸出:X XXXXXX X",其中 X 是一個數字,必須存在.

output: "X XXXXXX X" where X is a digit, must be present.

示例:1234567 => 0 123456 7

example: 1234567 => 0 123456 7

我嘗試了什么:

DecimalFormatSymbols group = new DecimalFormatSymbols();  
group.setGroupingSeparator(' '); 
DecimalFormat idFormat = new DecimalFormat("0,000000,0", group);

但這會打印出類似0 1 2 3 4 5 6 7"的內容:S 我做錯了什么?

But this prints something like "0 1 2 3 4 5 6 7" instead :S What am I doing wrong?

如果我這樣做,我可以打印我需要的東西:

I can print what I need if I do this:

DecimalFormatSymbols group = new DecimalFormatSymbols();  
group.setGroupingSeparator(' ');
group.setDecimalSeparator(' ');
DecimalFormat idFormat = new DecimalFormat("0,000000.0", group);

通過重新閱讀手冊,我意識到 DecimalFormat 沒有辦法打印可變長度組(幸運的是我只需要 2 個 - 所以我可以使用小數部分).但是你將如何正確"地做到這一點?是否可以在這里使用正則表達式/編寫我自己的函數,或者是否有庫已經這樣做了?

And from re-reading the manual, I realized that DecimalFormat doesn't have a way to print variable length groups (I'm lucky I only need 2 - so I can use fraction part). But how would you do this "properly"? Would it be OK to use regular expression here / write my own function, or are there libraries that do this already?

只是為了好玩,下面是基于正則表達式的方法:)

Just for kicks, below is the regex-based way of doing it :)

Random random = new Random();
System.out.println(
      String.valueOf(Math.round(random.nextDouble() * 1e8))
      .replaceAll("(.*)(\d{6})(\d)$", "$1 $2 $3")
      .replaceAll("^ ", "0 "));

推薦答案

我認為您不能為此使用 DecimalFormat 分組分隔符.來自 Javadoc:

I don't think you can use the DecimalFormat grouping separator for this. From the Javadoc:

如果您提供具有多個分組字符的模式,則最后一個和整數末尾之間的間隔就是所使用的間隔.所以 "#,##,###,####" == "######,####" == "##,####,####".

If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".

這篇關于DecimalFormat 可變組大小的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 亚洲精品视频在线观看免费 | 国产视频欧美 | 欧美性视频在线播放 | 黄色免费看| 91在线免费视频 | 在线视频成人 | 99精品国产在热久久 | 精品一区二区在线观看 | 国产98色在线 | 日韩 | 欧美成人免费电影 | 香蕉国产在线视频 | 日韩视频中文字幕 | 欧美精品啪啪 | 成人精品在线观看 | 日韩aⅴ视频 | 亚洲高清视频一区二区 | 欧美91| 中文字幕免费在线 | 日韩一区二区三区视频 | 妹子干综合 | 四虎影 | 中文字幕亚洲精品 | 99re国产视频| 91久久精品视频 | 天天干夜夜操 | 日韩二 | 亚洲情综合五月天 | 91在线视频播放 | 欧美亚洲网站 | 91p在线观看 | 亚洲成人久久久 | 青青久久| 精品1区 | 国产精品1区2区3区 国产在线观看一区 | 亚洲精品一区二区三区中文字幕 | 成人精品视频 | 91久久精品一区二区三区 | 一区二区三区免费观看 | 日韩电影中文字幕在线观看 | 欧美日韩91| 欧美一区二区三区久久精品 |