带有数字和字母的字符串,以加倍javafx

S.R*_*ose 3 javafx textfield

嗨,我试图从文本字段中读取数字,显示价格,例如£3.00,并将价格的值转换为双倍.有办法吗?

Double value;
value = Double.parseDouble(textField.getText()); 
Run Code Online (Sandbox Code Playgroud)

但由于英镑符号,它不会让我这样做.有没有办法剥去英镑符号然后读取数字.

谢谢

jew*_*sea 5

在JavaFX TextField API中内置了一些TextFormatter和更改过滤器处理逻辑,您可以使用它.

货币格式

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;

import java.text.DecimalFormat;
import java.text.ParseException;

class CurrencyFormatter extends TextFormatter<Double> {
    private static final double DEFAULT_VALUE = 5.00d;
    private static final String CURRENCY_SYMBOL = "\u00A3"; // british pound

    private static final DecimalFormat strictZeroDecimalFormat  
        = new DecimalFormat(CURRENCY_SYMBOL + "###,##0.00");

    CurrencyFormatter() {
        super(
                // string converter converts between a string and a value property.
                new StringConverter<Double>() {
                    @Override
                    public String toString(Double value) {
                        return strictZeroDecimalFormat.format(value);
                    }

                    @Override
                    public Double fromString(String string) {
                        try {
                            return strictZeroDecimalFormat.parse(string).doubleValue();
                        } catch (ParseException e) {
                            return Double.NaN;
                        }
                    }
                },
                DEFAULT_VALUE,
                // change filter rejects text input if it cannot be parsed.
                change -> {
                    try {
                        strictZeroDecimalFormat.parse(change.getControlNewText());
                        return change;
                    } catch (ParseException e) {
                        return null;
                    }
                }
        );
    }
}

public class FormattedTextField extends Application {

    public static void main(String[] args) { launch(args); }

    @Override
    public void start(final Stage stage) {
        TextField textField = new TextField();
        textField.setTextFormatter(new CurrencyFormatter());

        Label text = new Label();
        text.textProperty().bind(
                Bindings.concat(
                        "Text: ",
                        textField.textProperty()
                )
        );

        Label value = new Label();
        value.textProperty().bind(
                Bindings.concat(
                        "Value: ",
                        textField.getTextFormatter().valueProperty().asString()
                )
        );

        VBox layout = new VBox(
                10,
                textField,
                text,
                value,
                new Button("Apply")
        );
        layout.setPadding(new Insets(10));

        stage.setScene(new Scene(layout));
        stage.show();
    }

}
Run Code Online (Sandbox Code Playgroud)

如果您非常关注用户体验,DecimalFormat和过滤器的确切规则可能会有点棘手(例如,用户是否可以输入货币符号?如果用户未输入货币符号会发生什么?是否允许空值?等等以上示例在合理的用户体验和(相对)易于编程的解决方案之间提供折衷.对于实际的生产级应用程序,您可能希望稍微调整逻辑和行为以适合您的特定应用程序.

请注意,"应用"按钮实际上不需要执行任何操作来应用更改.当用户将焦点从文本字段更改时(只要它们通过更改过滤器),就会应用更改.因此,如果用户单击"应用"按钮,它将获得焦点,文本字段将失去焦点,并且如果适用,则应用更改.

上面的示例将货币值视为双精度(与问题匹配),但那些认真对待货币的人可能希望查看BigDecimal.

有关使用类似概念的更简单的解决方案,另请参阅: