在GWT中将字符串转换为BigDecimal [英] Converting String to BigDecimal in GWT

查看:118
本文介绍了在GWT中将字符串转换为BigDecimal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的GWT Web应用程序中,我有一个保存价格的文本框。
如何将字符串转换为BigDecimal?

解决方案

最简单的方法是创建新的文本框小部件继承ValueBox。
如果你这样做,你不必手动转换任何字符串值。 ValueBox负责处理所有的事情。



要获得BigDecimal值,您可以直接进入:

BigDecimal value = myTextBox.getValue();



您的 BigDecimalBox.java p>

  public class BigDecimalBox extends ValueBox< BigDecimal> {
public BigDecimalBox(){
super(Document.get()。createTextInputElement(),BigDecimalRenderer.instance(),
BigDecimalParser.instance());
}
}

然后,您的 BigDecimalRenderer.java

  public class BigDecimalRenderer extends AbstractRenderer< BigDecimal> {
private static BigDecimalRenderer INSTANCE;

public static Renderer< BigDecimal> instance(){
if(INSTANCE == null){
INSTANCE = new BigDecimalRenderer();
}
return INSTANCE;
}

protected BigDecimalRenderer(){
}

public String render(BigDecimal object){
if(null == object) {
return;
}

return NumberFormat.getDecimalFormat()。format(object);
}
}

而您的 BigDecimalParser.java

  package com.google.gwt.text.client; 

import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.text.shared.Parser;

import java.text.ParseException;

public class BigDecimalParser实现Parser< BigDecimal> {

private static BigDecimalParser INSTANCE;

public static Parser< BigDecimal> instance(){
if(INSTANCE == null){
INSTANCE = new BigDecimalParser();
}
return INSTANCE;
}
$ b保护BigDecimalParser(){
}

公共BigDecimal解析(CharSequence对象)抛出ParseException {
if(。 equals(object.toString())){
return null;
}

尝试{
return new BigDecimal(object.toString());
} catch(NumberFormatException e){
throw new ParseException(e.getMessage(),0);
}
}
}


In my GWT web application I have a textbox that holds a price. How can one convert that String to a BigDecimal?

解决方案

The easiest way is to create new text box widget that inherits ValueBox. If you do it this way, you won't have to convert any string values manually. the ValueBox takes care of it all.

To get the BigDecimal value entered you can just go:

BigDecimal value = myTextBox.getValue();

Your BigDecimalBox.java:

public class BigDecimalBox extends ValueBox<BigDecimal> {
  public BigDecimalBox() {
    super(Document.get().createTextInputElement(), BigDecimalRenderer.instance(),
        BigDecimalParser.instance());
  }
}

Then your BigDecimalRenderer.java

public class BigDecimalRenderer extends AbstractRenderer<BigDecimal> {
  private static BigDecimalRenderer INSTANCE;

  public static Renderer<BigDecimal> instance() {
    if (INSTANCE == null) {
      INSTANCE = new BigDecimalRenderer();
    }
    return INSTANCE;
  }

  protected BigDecimalRenderer() {
  }

  public String render(BigDecimal object) {
    if (null == object) {
      return "";
    }

    return NumberFormat.getDecimalFormat().format(object);
  }
}

And your BigDecimalParser.java

package com.google.gwt.text.client;

import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.text.shared.Parser;

import java.text.ParseException;

public class BigDecimalParser implements Parser<BigDecimal> {

  private static BigDecimalParser INSTANCE;

  public static Parser<BigDecimal> instance() {
    if (INSTANCE == null) {
      INSTANCE = new BigDecimalParser();
    }
    return INSTANCE;
  }

  protected BigDecimalParser() {
  }

  public BigDecimal parse(CharSequence object) throws ParseException {
    if ("".equals(object.toString())) {
      return null;
    }

    try {
      return new BigDecimal(object.toString());
    } catch (NumberFormatException e) {
      throw new ParseException(e.getMessage(), 0);
    }
  }
}

这篇关于在GWT中将字符串转换为BigDecimal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆