首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Calculator:如何防止多个小数?

React Calculator:如何防止多个小数?
EN

Stack Overflow用户
提问于 2019-10-16 00:46:35
回答 3查看 299关注 0票数 1

我几乎完成了使用React构建一个简单的计算器。我只是对多个小数有点问题。我想要做的是写一个条件,但它不起作用。你能帮帮我吗?

下面是我的代码的一部分:

代码语言:javascript
复制
class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            input: '0'
        };
    }


    addToInput = e => {
        const value = e.target.value;
        const oldValue = this.state.input;

        if (this.state.input != '0') {
            this.setState({ input: (this.state.input + value) });

        } else if (value == '.' && oldValue.includes('.')) {
            console.log('Mulitple decimals');
        } else {
            this.setState({ input: value });
        }
    };

    render() {
        return (<Button addToInput={ this.addToInput } />);
    }
}

class Button extends React.Component {

    render() {
        return (

            <div className="row">
                <button
                    value="."
                    id="decimal"
                    onClick={ this.props.addToInput }
                >.
                </button>
                <button
                    value="0"
                    id="zero"
                    onClick={ this.props.addToInput }
                >0
                </button>
                <button
                    value="-"
                    id="subtract"
                    onClick={ this.props.addToInput }
                >-
                </button>
            </div>


        );
    }
}

提前谢谢你!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-10-16 14:36:51

将您的addToInput更改为:

代码语言:javascript
复制
    addToInput = e => {
        const value = e.target.value;
        const oldValue = this.state.input;

        if (value === '.' && oldValue.includes('.')) {
            console.log('Mulitple decimals');
            return;
        }

        if (this.state.input !== '0') {
            this.setState({ input: (this.state.input + value) });

        } else {
            this.setState({ input: value });
        }
    };

为什么你会遇到这样的问题:

代码语言:javascript
复制
    addToInput = e => {
        const value = e.target.value;
        const oldValue = this.state.input;

        if (this.state.input !== '0') {
            // after first addToInput you will end up here ALWAYS
            this.setState({ input: (this.state.input + value) });
        } else if (value === '.' && oldValue.includes('.')) {
            // You will never be here because this.state.input !== '0' is always true after first addToInput
            console.log('Mulitple decimals'); 
        } else {
            // you will end up here only when you lunch your addToInput first time
            this.setState({ input: value });
        }
    };
票数 0
EN

Stack Overflow用户

发布于 2019-10-16 09:40:06

您可以查看传入的值,检查它是否是一个.,并检查输入是否已经有一个。如果是,则不执行任何操作,否则将该值添加到输入的末尾:

代码语言:javascript
复制
addToInput = e => {
  const { value } = e.target;
  const { input } = this.state;

  if (value === "." && input.includes(".")) {
    return;
  }

  this.setState({ input: `${input}${value}` });
};
票数 0
EN

Stack Overflow用户

发布于 2019-10-16 15:48:48

好的,正则表达式会对你有所帮助。

代码语言:javascript
复制
const regex = /^[1-9]\d*(\.\d+)?$/;

然后,您可以检查您的值:

代码语言:javascript
复制
regex.test('222') // true
regex.test('222.') // false
regex.test('222.0') // true
regex.test('222.0.1') // false
regex.test('222.01234') // true
regex.test('abc') // false
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58399297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档