Java Code Examples for org.springframework.format.number.NumberStyleFormatter

Following code examples demonstrate how to use org.springframework.format.number.NumberStyleFormatterfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.format.number.NumberStyleFormatterand various code implementation of this class.

	public void addFormatters(FormatterRegistry registry) {
		
		NumberStyleFormatter decimalFormatter = new NumberStyleFormatter("#,##0.00");
		registry.addFormatterForFieldType(BigDecimal.class, decimalFormatter);
	
		NumberStyleFormatter integerFormatter = new NumberStyleFormatter("#,##0");
		registry.addFormatterForFieldType(Integer.class, integerFormatter);
		
		registry.addConverter(new CategoryConverter());
		registry.addConverter(new GroupConverter());
	} 


	public FormattingConversionService mvcConversionService() {
		DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
	
		
		
		NumberStyleFormatter bigDecimalFormatter = new NumberStyleFormatter("#,##0.00");
		conversionService.addFormatterForFieldType(BigDecimal.class, bigDecimalFormatter);
		
		NumberStyleFormatter integerFormatter = new NumberStyleFormatter("#,##0");
		conversionService.addFormatterForFieldType(Integer.class, integerFormatter);
		
		return conversionService;
	} 

    private Formatter<Number> configureFormatterFrom(NumberFormat annotation, Class<?> fieldType) {
        if (!annotation.pattern().isEmpty()) {
            return new NumberStyleFormatter(annotation.pattern());
        } else {
            NumberFormat.Style style = annotation.style();
            if (style == NumberFormat.Style.PERCENT) {
                return new PercentStyleFormatter();
            } else if (style == NumberFormat.Style.CURRENCY) {
                return new CurrencyStyleFormatter();
            } else {
                return new NumberStyleFormatter();
            }
        }
    } 

	public void addFormatters(FormatterRegistry registry) {
		registry.addConverter(new EstiloConverter());
		
		NumberStyleFormatter bigDecimalFormatter = new NumberStyleFormatter("#,##0.00");
		registry.addFormatterForFieldType(BigDecimal.class, bigDecimalFormatter);
		
		NumberStyleFormatter integerFormatter = new NumberStyleFormatter("#,##0");
		registry.addFormatterForFieldType(Integer.class, integerFormatter);
	} 

	private Formatter<Number> createFormatter(NumberFormat annotation, Class<?> fieldType) {
		if (!annotation.pattern().isEmpty()) {
			return new NumberStyleFormatter(annotation.pattern());
		} else {
			Style style = annotation.style();
			if (style == Style.PERCENT) {
				return new PercentStyleFormatter();
			} else if (style == Style.CURRENCY) {
				return new CurrencyStyleFormatter();
			} else {
				return new NumberStyleFormatter();
			}
		}
	} 

	public FormattingConversionService mvcConversionService() {
		
		System.out.println(">>>>>>>>>>> REGISTROU...");
		
		DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
		conversionService.addConverter(new CategoryConverter());
		conversionService.addConverter(new GroupConverter());
		
		NumberStyleFormatter bigDecimalFormatter = new NumberStyleFormatter("#,##0.00");
		conversionService.addFormatterForFieldType(BigDecimal.class, bigDecimalFormatter);
		
		NumberStyleFormatter integerFormatter = new NumberStyleFormatter("#,##0");
		conversionService.addFormatterForFieldType(Integer.class, integerFormatter);
		
		
		return conversionService;
	} 

	public void addFormatters(FormatterRegistry registry) {
		NumberStyleFormatter numberFormatter = new NumberStyleFormatter();
		numberFormatter.setPattern("#,###,###,###.##");
		registry.addFormatterForFieldType(Number.class, numberFormatter);
	} 

	public String getAsText() {
		NumberStyleFormatter nf = (NumberStyleFormatter) getValue();
		if (nf == null) {
			return "";
		}
		return originalValue;
	} 

	public void setAsText(String text) {
		setValue(text == null ? null : new NumberStyleFormatter(text));
		originalValue = text;
	} 

	public void addFormatters(FormatterRegistry registry) {

		NumberStyleFormatter bigDecimalFormatter = new NumberStyleFormatter("#,##0.00");
		registry.addFormatterForFieldType(BigDecimal.class, bigDecimalFormatter);

		NumberStyleFormatter integerFormatter = new NumberStyleFormatter("#,##0");
		registry.addFormatterForFieldType(Integer.class, integerFormatter);

		super.addFormatters(registry);
	} 

	public void customizeBinding3(WebDataBinder binder) {
		NumberStyleFormatter numberFormatter = new NumberStyleFormatter();
		numberFormatter.setPattern("#,###,###,###.##");
		binder.addCustomFormatter(numberFormatter, "price");
	} 

Advertisement
Javadoc
A general-purpose number formatter using NumberFormat's number style.

Delegates to java.t

ext.NumberFormat#getInstance(Locale). Configures BigDecimal parsing so there is no loss in precision. Allows configuration over the decimal number pattern. The #parse(String, Locale) routine always returns a BigDecimal. @author Keith Donald @author Juergen Hoeller @since 4.2 @see #setPattern @see #setLenient

Read More
Advertisement