栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java-十进制Format.parse返回具有指定小数位数的双精度值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java-十进制Format.parse返回具有指定小数位数的双精度值

采纳您所说的内容,我对代码进行了一些修改,以涵盖不同的语言环境。关键是将本地化格式的值字符串转换为Double,然后根据格式字符串将其四舍五入。

格式字符串始终是基于英国的格式,十进制分隔符指定为“。” 和千位分隔符指定为“,”。

我正在使用DecimalFormat最初基于指定的语言环境解析本地化格式。这样可以正确给出字符串的Double等效值。然后,我使用BigDecimal处理舍入。我可以从DecimalFormat实例中获取小数位数,然后在BigDecimal上调用setScale进行舍入。

初始代码结构已被修改,以允许您查看在不同的语言环境下发生的情况,这感谢@ RD01注意其他语言环境的重要性。

我现在有如下代码:

private void runTests3() {    // output current locale we are running under    System.out.println( "Current Locale is " + Locale.getDefault().toString() );    // number in Central European Format with a format string specified in UK format    String numbersInEuropeanFormatString[] = new String[] { "1.000,234567", "1,2345678", "1.222.333,234567" };    String formatUK = "###,##0.0000";    // output numbers using the german locale    System.out.println("Output numbers using the German localen");    for(String num : numbersInEuropeanFormatString ) {        formatNumberAsDouble(num, formatUK, Locale.GERMAN);    }    // output numbers using the UK locale.      // this should return unexpected results as the number is in European format    System.out.println("Output numbers using the UK localen");    for(String num : numbersInEuropeanFormatString ) {        formatNumberAsDouble(num, formatUK, Locale.UK);    }    // output numbers using new DecimalFormat( formatUK ) - no locale specified    System.out.println("nnOutput numbers using new DecimalFormat( " + formatUK + " )n");    for(String num : numbersInEuropeanFormatString ) {        formatNumberAsDouble( num, formatUK, null);    }}private void formatNumberAsDouble(String value, String format, Locale locale) {    NumberFormat formatter;    int decimalPlaces;    // create the formatter based on the specified locale    if( locale != null ) {         formatter = NumberFormat.getNumberInstance(locale);         // creating the above number format does not take in the format string         // so create a new one that we won't use at all just to get the         // decimal places in it         decimalPlaces = (new DecimalFormat(format)).getMaximumFractionDigits();    } else {        formatter = new DecimalFormat( format );        decimalPlaces = formatter.getMaximumFractionDigits();    }    // get the result as number    Double result = null;    try {        result = formatter.parse( value ).doublevalue();    } catch( ParseException ex ) {        // not bothered at minute    }    // round the Double to the precision specified in the format string    BigDecimal bd = new BigDecimal(result );    Double roundedValue = bd.setScale( decimalPlaces, RoundingMode.HALF_UP ).doublevalue();    // output summary    System.out.println("tValue = " + value);    System.out.println( locale == null  ? "tLocale not specified" : "tLocale = " + locale.toString());    System.out.println( format == null || format.length() == 0 ? "tFormat = Not specified" : "tFormat = " + format);    System.out.println("tResult (Double) = " + result);    System.out.println("tRounded Result (Double) (" + decimalPlaces + "dp) = " + roundedValue);    System.out.println("");}

这将产生以下输出:

Current Locale is nl_BEOutput numbers using the German locale    Value = 1.000,234567    Locale = de    Format = ###,##0.0000    Result (Double) = 1000.234567    Rounded Result (Double) (4dp) = 1000.2346    Value = 1,2345678    Locale = de    Format = ###,##0.0000    Result (Double) = 1.2345678    Rounded Result (Double) (4dp) = 1.2346    Value = 1.222.333,234567    Locale = de    Format = ###,##0.0000    Result (Double) = 1222333.234567    Rounded Result (Double) (4dp) = 1222333.2346Output numbers using the UK locale    Value = 1.000,234567    Locale = en_GB    Format = ###,##0.0000    Result (Double) = 1.0    Rounded Result (Double) (4dp) = 1.0    Value = 1,2345678    Locale = en_GB    Format = ###,##0.0000    Result (Double) = 1.2345678E7    Rounded Result (Double) (4dp) = 1.2345678E7    Value = 1.222.333,234567    Locale = en_GB    Format = ###,##0.0000    Result (Double) = 1.222    Rounded Result (Double) (4dp) = 1.222Output numbers using new DecimalFormat( ###,##0.0000 )    Value = 1.000,234567    Locale not specified    Format = ###,##0.0000    Result (Double) = 1000.234567    Rounded Result (Double) (4dp) = 1000.2346    Value = 1,2345678    Locale not specified    Format = ###,##0.0000    Result (Double) = 1.2345678    Rounded Result (Double) (4dp) = 1.2346    Value = 1.222.333,234567    Locale not specified    Format = ###,##0.0000    Result (Double) = 1222333.234567    Rounded Result (Double) (4dp) = 1222333.2346


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430987.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号