我是靠谱客的博主 害怕唇膏,这篇文章主要介绍java字符型转百分比_如何使用DecimalFormat将表示例如百分比,货币的数字值转换为字符串并返回BigDecimal...,现在分享给大家,希望可以做个参考。

过了一会儿写了这个。它似乎有效,但我不确定它是否能在所有时间都正常工作。也许它可以帮助别人。我也希望它不违反任何许可证)。

import java.math.BigDecimal;

import java.text.DecimalFormat;

import java.text.DecimalFormatSymbols;

import java.text.ParseException;

import java.util.Currency;

import java.util.Locale;

import com.ibm.icu.impl.ICUResourceBundle;

import com.ibm.icu.text.NumberFormat;

import com.ibm.icu.util.UResourceBundle;

public class NumericHelper {

public static BigDecimal parseStringToBigDecimal( String value, DecimalFormat decimalFormat ) throws ParseException {

decimalFormat.setParseBigDecimal( true );

return ( BigDecimal ) decimalFormat.parse( value );

}

/**

* Extracted code from {@link NumberFormat#getPattern()} and

* {@link java.text.NumberFormat#getInstance(Locale desiredLocale, int choice)} with some

* modifications to get DecimalFormat with good Pattern

*

* @param choice is static values from class {@link NumberFormat} eg

* {@link NumberFormat#INTEGERSTYLE}

*/

public static DecimalFormat getDecimalFormatWithPattern( Locale desiredLocale, int choice ) {

ICUResourceBundle rb = ( ICUResourceBundle ) UResourceBundle.getBundleInstance( ICUResourceBundle.ICU_BASE_NAME, desiredLocale );

String[] numberPatterns = rb.getStringArray( "NumberPatterns" );

DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance( desiredLocale );

int entry = ( choice == NumberFormat.INTEGERSTYLE ) ? NumberFormat.NUMBERSTYLE : choice;

DecimalFormat format = new DecimalFormat( numberPatterns[entry], symbols );

if ( choice == NumberFormat.INTEGERSTYLE ) {

format.setMaximumFractionDigits( 0 );

format.setDecimalSeparatorAlwaysShown( false );

format.setParseIntegerOnly( true );

}

else if ( choice == NumberFormat.CURRENCYSTYLE ) {

adjustForCurrencyDefaultFractionDigits( format );

}

return format;

}

/**

* Extracted from {@link DecimalFormat#adjustForCurrencyDefaultFractionDigits()} because it can

* not be called

*/

protected static void adjustForCurrencyDefaultFractionDigits( DecimalFormat decimalFormat ) {

DecimalFormatSymbols symbols = decimalFormat.getDecimalFormatSymbols();

Currency currency = symbols.getCurrency();

if ( currency == null ) {

try {

currency = Currency.getInstance( symbols.getInternationalCurrencySymbol() );

}

catch ( IllegalArgumentException e ) {

}

}

if ( currency != null ) {

int digits = currency.getDefaultFractionDigits();

if ( digits != -1 ) {

int oldMinDigits = decimalFormat.getMinimumFractionDigits();

// Common patterns are "#.##", "#.00", "#".

// Try to adjust all of them in a reasonable way.

if ( oldMinDigits == decimalFormat.getMaximumFractionDigits() ) {

decimalFormat.setMinimumFractionDigits( digits );

decimalFormat.setMaximumFractionDigits( digits );

}

else {

decimalFormat.setMinimumFractionDigits( Math.min( digits, oldMinDigits ) );

decimalFormat.setMaximumFractionDigits( digits );

}

}

}

}

}在下一个时间之后,我想到了NumberFormat.get *** Instance() b>方法,它返回 b>它的后代类(DecimalFormat b>)。我不确定这是如何工作的,但它确实有效。在代码是时刻,试图从LocaleServiceProviderPool获取格式,所以我相信它也返回DecimalFormat,如果确实如此,那就没有问题使用这个原始方法并转换为DecimalFormat但是 b>如果不是,你必须检查它< / b>。在代码中我写的是这部分省略了

最后

以上就是害怕唇膏最近收集整理的关于java字符型转百分比_如何使用DecimalFormat将表示例如百分比,货币的数字值转换为字符串并返回BigDecimal...的全部内容,更多相关java字符型转百分比_如何使用DecimalFormat将表示例如百分比,货币内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(136)

评论列表共有 0 条评论

立即
投稿
返回
顶部