日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ChoiceFormat:数字范围格式

發(fā)布時(shí)間:2023/12/3 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ChoiceFormat:数字范围格式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ChoiceFormat類的Javadoc聲明ChoiceFormat “允許您將格式附加到一定范圍的數(shù)字上”,并且“通常在MessageFormat中用于處理復(fù)數(shù)”。 這篇文章描述了java.text.ChoiceFormat并提供了一些在Java代碼中應(yīng)用它的示例。

ChoiceFormat與java.text包中其他“ 格式 ”類之間最明顯的區(qū)別之一是ChoiceFormat不提供用于訪問ChoiceFormat實(shí)例的靜態(tài)方法。 相反, ChoiceFormat提供了兩個(gè)用于實(shí)例化ChoiceFormat對象的構(gòu)造函數(shù)。 ChoiceFormat的Javadoc著重說明了這一點(diǎn):


ChoiceFormat與其他Format類的不同之處在于,您使用構(gòu)造函數(shù)(而不是使用getInstance樣式工廠方法)創(chuàng)建ChoiceFormat對象。 不需要工廠方法,因?yàn)镃hoiceFormat不需要為給定語言環(huán)境進(jìn)行任何復(fù)雜的設(shè)置。 實(shí)際上, ChoiceFormat不會實(shí)現(xiàn)任何特定ChoiceFormat語言環(huán)境的行為。

用兩個(gè)數(shù)組構(gòu)造ChoiceFormat

ChoiceFormat提供的兩個(gè)構(gòu)造函數(shù)中的第一個(gè)接受兩個(gè)數(shù)組作為其參數(shù)。 第一個(gè)數(shù)組是原始雙精度數(shù)組,表示每個(gè)間隔的最小值(起始值)。 第二個(gè)數(shù)組是一個(gè)字符串?dāng)?shù)組,代表與每個(gè)時(shí)間間隔關(guān)聯(lián)的名稱。 兩個(gè)數(shù)組必須具有相同數(shù)量的元素,因?yàn)樵跀?shù)字(雙精度)間隔和描述這些間隔的字符串之間存在假定的一對一映射。 如果兩個(gè)數(shù)組的元素?cái)?shù)量不同,則會遇到以下異常。


線程“主”中的異常java.lang.IllegalArgumentException:數(shù)組和限制數(shù)組的長度必須相同。

ChoiceFormat(double [],String [])構(gòu)造函數(shù)的Javadoc說明第一個(gè)數(shù)組參數(shù)名為“ limits”,類型為double[] ,并被描述為“升序限制”。 第二個(gè)數(shù)組參數(shù)名為“ formats”,類型為String[] ,并被描述為“相應(yīng)的格式字符串”。 根據(jù)Javadoc,此構(gòu)造函數(shù)“使用限制和相應(yīng)的格式進(jìn)行構(gòu)造”。

下一個(gè)代碼清單中演示了使用ChoiceFormat構(gòu)造函數(shù)接受兩個(gè)數(shù)組參數(shù)的writeGradeInformation(ChoiceFormat)稍后將顯示writeGradeInformation(ChoiceFormat)方法和fredsTestScores變量)。

/*** Demonstrate ChoiceFormat instantiated with ChoiceFormat* constructor that accepts an array of double and an array* of String.*/ public void demonstrateChoiceFormatWithDoublesAndStringsArrays() {final double[] minimumPercentages = {0, 60, 70, 80, 90};final String[] letterGrades = {"F", "D", "C", "B", "A"};final ChoiceFormat gradesFormat = new ChoiceFormat(minimumPercentages, letterGrades);writeGradeInformation(fredsTestScores, gradesFormat); }

上面的示例滿足了圖示的ChoiceFormat構(gòu)造函數(shù)的期望。 這兩個(gè)數(shù)組的元素?cái)?shù)相同,第一個(gè)( double[] )數(shù)組的元素按升序排列,第二個(gè)( String[] )數(shù)組的“格式”與相應(yīng)的間隔開始限制值相同在第一個(gè)數(shù)組中。

上面的代碼片段中引用的writeGradeInformation(ChoiceFormat)方法演示了如何使用基于兩個(gè)數(shù)組的ChoiceFormat實(shí)例來“格式化”所提供的數(shù)值作為字符串。 接下來顯示該方法的實(shí)現(xiàn)。

/*** Write grade information to standard output* using the provided ChoiceFormat instance.** @param testScores Test Scores to be displayed with formatting.* @param gradesFormat ChoiceFormat instance to be used to format output.*/ public void writeGradeInformation(final Collection<Double> testScores,final ChoiceFormat gradesFormat) {double sum = 0;for (final Double testScore : testScores){sum += testScore;out.println(testScore + " is a '" + gradesFormat.format(testScore) + "'.");}double average = sum / testScores.size();out.println("The average score (" + average + ") is a '"+ gradesFormat.format(average) + "'."); }

上面的代碼使用提供的ChoiceFormat實(shí)例來“格式化”測試成績。 “格式”不是打印數(shù)字值,而是打印與數(shù)字值所在的時(shí)間間隔關(guān)聯(lián)的字符串。 下一個(gè)代碼清單顯示了這些示例中使用的fredsTestScores的定義。

private static List<Double> fredsTestScores; static {final ArrayList<Double> scores = new ArrayList<>();scores.add(75.6);scores.add(88.8);scores.add(97.3);scores.add(43.3);fredsTestScores = Collections.unmodifiableList(scores); }

通過用兩個(gè)數(shù)組實(shí)例化的ChoiceFormat實(shí)例運(yùn)行這些測試分?jǐn)?shù),將產(chǎn)生以下輸出:

75.6 is a 'C'. 88.8 is a 'B'. 97.3 is a 'A'. 43.3 is a 'F'. The average score (76.25) is a 'C'.

使用模式字符串構(gòu)造ChoiceFormat

接受基于字符串的模式的ChoiceFormat(String)構(gòu)造函數(shù)可能對那些愿意將基于字符串的模式與類似格式類(例如DateFormat和DecimalFormat)一起使用的開發(fā)人員更具吸引力。 下一個(gè)代碼清單演示了此構(gòu)造函數(shù)的用法。 提供給構(gòu)造函數(shù)的模式將導(dǎo)致ChoiceFormat實(shí)例,該實(shí)例的格式應(yīng)與前面示例中使用帶有兩個(gè)數(shù)組的構(gòu)造函數(shù)創(chuàng)建的ChoiceFormat實(shí)例相同。

/*** Demonstrate ChoiceFormat instantiated with ChoiceFormat* constructor that accepts a String pattern.*/ public void demonstrateChoiceFormatWithStringPattern() {final String limitFormatPattern = "0#F | 60#D | 70#C | 80#B | 90#A";final ChoiceFormat gradesFormat = new ChoiceFormat(limitFormatPattern);writeGradeInformation(fredsTestScores, gradesFormat); }

此處調(diào)用的writeGradeInformation方法與之前調(diào)用的方法相同,并且輸出也相同(此處未顯示,因?yàn)橄嗤?#xff09;。

極限和邊界上的ChoiceFormat行為

到目前為止,這些示例在預(yù)期范圍內(nèi)的測試成績上都運(yùn)行良好。 現(xiàn)在將使用另一組測試成績來演示ChoiceFormat其他一些功能。 這組新的測試分?jǐn)?shù)將在下一個(gè)代碼列表中設(shè)置,其中包括“不可能”的否定分?jǐn)?shù)和另一個(gè)“可能”高于100的分?jǐn)?shù)。

private static List<Double> boundaryTestScores; static {final ArrayList<Double> boundaryScores = new ArrayList<Double>();boundaryScores.add(-25.0);boundaryScores.add(0.0);boundaryScores.add(20.0);boundaryScores.add(60.0);boundaryScores.add(70.0);boundaryScores.add(80.0);boundaryScores.add(90.0);boundaryScores.add(100.0);boundaryScores.add(115.0);boundaryTestScores = boundaryScores; }

當(dāng)以上測試分?jǐn)?shù)集通過之前創(chuàng)建的兩個(gè)ChoiceFormat實(shí)例運(yùn)行時(shí),輸出如下所示。

-25.0 is a 'F '. 0.0 is a 'F '. 20.0 is a 'F '. 60.0 is a 'D '. 70.0 is a 'C '. 80.0 is a 'B '. 90.0 is a 'A'. 100.0 is a 'A'. 115.0 is a 'A'. The average score (56.666666666666664) is a 'F '.

剛剛顯示的輸出表明ChoiceFormat構(gòu)造函數(shù)中設(shè)置的“限制”是“包含的”,這意味著這些限制適用于指定的限制和更高的限制(直到下一個(gè)限制)。 換句話說,數(shù)字范圍被定義為大于或等于指定的限制。 ChoiceFormat的Javadoc文檔使用數(shù)學(xué)描述對此進(jìn)行了描述:


當(dāng)且僅當(dāng)limit [j]≤X <limit [j + 1]時(shí),X與j匹配

邊界測試分?jǐn)?shù)示例的輸出還演示了Javadoc文檔中描述的ChoiceFormat另一個(gè)特征:“如果不匹配,則使用第一個(gè)或最后一個(gè)索引,具體取決于數(shù)字(X)太低還是太低”高?!?因?yàn)樵谔峁┑腃hoiceFormat實(shí)例中不存在-25.0的匹配項(xiàng),所以將最低范圍(最低限度為0的“ F”)應(yīng)用于低于最低范圍的那個(gè)數(shù)字。 在這些測試成績示例中,沒有為“ A”指定比“ 90”更高的限制,因此所有高于90的分?jǐn)?shù)(包括高于100的分?jǐn)?shù))都針對“ A”。 假設(shè)我們想要將分?jǐn)?shù)范圍強(qiáng)制在0到100之間,或者對于小于0或大于100的分?jǐn)?shù),將格式化結(jié)果指示為“無效”。這可以如下面的代碼清單所示。

/*** Demonstrating enforcing of lower and upper boundaries* with ChoiceFormat instances.*/ public void demonstrateChoiceFormatBoundariesEnforced() {// Demonstrating boundary enforcement with ChoiceFormat(double[], String[])final double[] minimumPercentages = {Double.NEGATIVE_INFINITY, 0, 60, 70, 80, 90, 100.000001};final String[] letterGrades = {"Invalid - Too Low", "F", "D", "C", "B", "A", "Invalid - Too High"};final ChoiceFormat gradesFormat = new ChoiceFormat(minimumPercentages, letterGrades);writeGradeInformation(boundaryTestScores, gradesFormat);// Demonstrating boundary enforcement with ChoiceFormat(String)final String limitFormatPattern = "-\u221E#Invalid - Too Low | 0#F | 60#D | 70#C | 80#B | 90#A | 100.0<Invalid - Too High";final ChoiceFormat gradesFormat2 = new ChoiceFormat(limitFormatPattern);writeGradeInformation(boundaryTestScores, gradesFormat2); }

執(zhí)行上述方法時(shí),其輸出顯示兩種方法都更好地執(zhí)行了邊界條件。

-25.0 is a 'Invalid - Too Low'. 0.0 is a 'F'. 20.0 is a 'F'. 60.0 is a 'D'. 70.0 is a 'C'. 80.0 is a 'B'. 90.0 is a 'A'. 100.0 is a 'A'. 115.0 is a 'Invalid - Too High'. The average score (56.666666666666664) is a 'F'. -25.0 is a 'Invalid - Too Low '. 0.0 is a 'F '. 20.0 is a 'F '. 60.0 is a 'D '. 70.0 is a 'C '. 80.0 is a 'B '. 90.0 is a 'A '. 100.0 is a 'A '. 115.0 is a 'Invalid - Too High'. The average score (56.666666666666664) is a 'F '.

最后一個(gè)代碼清單演示了如何在每個(gè)示例中使用Double.NEGATIVE_INFINITY和\u221E ( Unicode INFINITY字符 )建立最低限度的邊界。 對于高于100.0的分?jǐn)?shù),如果將其格式化為無效分?jǐn)?shù),則基于數(shù)組的ChoiceFormat使用一個(gè)稍大于100的數(shù)字作為該無效范圍的下限。 基于字符串/模式的ChoiceFormat實(shí)例在使用小于號(<)來將“無效–過高”范圍的下限指定為大于100.0的任何數(shù)字時(shí),提供了更大的靈活性和準(zhǔn)確性。

使用ChoiceFormat處理無,單數(shù)和復(fù)數(shù)

我通過引用Javadoc來打開這篇文章,指出ChoiceFormat “通常在MessageFormat中用于處理復(fù)數(shù)”,但尚未在本文中演示這種常用用法。 為了完整起見 ,我將在此處非常簡短地演示其中的一部分(沒有MessageFormat的復(fù)數(shù)),但是Java教程的 “ 處理復(fù)數(shù)”課 ( 國際化的一部分)中提供了ChoiceFormat的這種常用用法的更完整的說明(具有MessageFormat的復(fù)數(shù))。 徑 )。

下一個(gè)代碼清單演示了ChoiceFormat在處理單數(shù)和復(fù)數(shù)情況下的應(yīng)用。

/*** Demonstrate ChoiceFormat used for differentiation of* singular from plural and none.*/ public void demonstratePluralAndSingular() {final double[] cactiLowerLimits = {0, 1, 2, 3, 4, 10};final String[] cactiRangeDescriptions ={"no cacti", "a cactus", "a couple cacti", "a few cacti", "many cacti", "a plethora of cacti"};final ChoiceFormat cactiFormat = new ChoiceFormat(cactiLowerLimits, cactiRangeDescriptions);for (int cactiCount = 0; cactiCount < 11; cactiCount++){out.println(cactiCount + ": I own " + cactiFormat.format(cactiCount) + ".");} }

運(yùn)行最后一個(gè)代碼清單中的示例將導(dǎo)致輸出,如下所示。

0: I own no cacti. 1: I own a cactus. 2: I own a couple cacti. 3: I own a few cacti. 4: I own many cacti. 5: I own many cacti. 6: I own many cacti. 7: I own many cacti. 8: I own many cacti. 9: I own many cacti. 10: I own a plethora of cacti.

ChoiceFormat的模式支持的最后一個(gè)符號

\u2264 ( ≤ )是ChoiceFormat模式分析可識別的另一種符號,用于根據(jù)生成的數(shù)值格式化字符串。 下一個(gè)代碼清單和該代碼清單后面的代碼輸出中對此進(jìn)行了演示。 請注意,在此示例中, \u2264工作原理與使用前面顯示的更簡單的#號相同。

/*** Demonstrate using \u2264 in String pattern for ChoiceFormat* to represent >= sign. Treated differently than less-than* sign but similarly to #.*/ public void demonstrateLessThanOrEquals() {final String limitFormatPattern = "0\u2264F | 60\u2264D | 70\u2264C | 80\u2264B | 90\u2264A";final ChoiceFormat gradesFormat = new ChoiceFormat(limitFormatPattern);writeGradeInformation(fredsTestScores, gradesFormat); }75.6 is a 'C '. 88.8 is a 'B '. 97.3 is a 'A'. 43.3 is a 'F '. The average score (76.25) is a 'C '.

評論中的意見

在本節(jié)中,我總結(jié)了本文及其示例過程中有關(guān)ChoiceFormat一些觀察。

  • 使用ChoiceFormat(double [],String [])構(gòu)造函數(shù)時(shí) ,兩個(gè)傳入的數(shù)組必須具有相同的大小,否則將引發(fā)IllegalArgumentException (“數(shù)組和限制數(shù)組必須具有相同的長度?!?#xff09;。
  • 提供給ChoiceFormat(double [],String [])構(gòu)造函數(shù)的“ limits” double[]數(shù)組應(yīng)具有從左到右以數(shù)字升序排列的限制。 如果不是這種情況,則不會引發(fā)任何異常,但是邏輯幾乎肯定不會正確,因?yàn)獒槍hoiceFormat實(shí)例進(jìn)行格式化的字符串將錯(cuò)誤地“匹配”。 同樣的期望也適用于接受模式的構(gòu)造函數(shù)。
  • ChoiceFormat允許Double.POSITIVE_INFINITY和Double.NEGATIVE_INFINITY通過其兩個(gè)數(shù)組的構(gòu)造函數(shù)用于指定范圍的下限。
  • ChoiceFormat允許\u221E和-\u221E通過其單個(gè)String(模式)構(gòu)造函數(shù)來指定較低的范圍限制。
  • ChoiceFormat構(gòu)造函數(shù)接受String模式比雙數(shù)組構(gòu)造函數(shù)靈活一些,并且允許將下限邊界指定為一定數(shù)量范圍內(nèi)的所有內(nèi)容,而不必確切地包含該特定數(shù)量。
  • 提供給單個(gè)String ChoiceFormat構(gòu)造函數(shù)的String模式中具有特殊含義的符號和字符包括# , < , \u2264 ( ≤ ), \u221E ( ∞ )和| 。

結(jié)論

ChoiceFormat允許自定義數(shù)字范圍的格式,以便特定范圍可以具有不同和特定的表示形式。 這篇文章涵蓋了使用ChoiceFormat進(jìn)行數(shù)字范圍格式化的幾個(gè)不同方面,但是這篇文章沒有涵蓋使用ChoiceFormat 從字符串中解析數(shù)字范圍 。

進(jìn)一步閱讀

  • ChoiceFormat API文檔
  • 處理復(fù)數(shù)
  • 文本:消息格式的自由–第2部分:選擇格式
  • 使用ChoiceFormat的Java i18n多元化
  • ChoiceFormat有什么問題? (翻譯后的內(nèi)容–第四部分)
  • 進(jìn)一步了解ChoiceFormat的問題

翻譯自: https://www.javacodegeeks.com/2014/09/choiceformat-numeric-range-formatting.html

總結(jié)

以上是生活随笔為你收集整理的ChoiceFormat:数字范围格式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。