Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

flutterのdartで作った電卓で小数点0.01が入力できない

解決したいこと

flutterで作った電卓で小数点0.01が入力できない

flitterのdartで電卓アプリを作成しています。
小数点の計算をしようと思い0.01と入力したいのですが、うまく出力されません例えば1 ÷ 100をしたら0.01と表示はされます。何か解決策があれば教えてほしいです。

または、問題・エラーが起きている画像をここにドラッグアンドドロップ

該当するソースコード

import 'package:flutter/material.dart';
import 'dart:math' as Math;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.pink,
      ),
      home: MyHomePage(title: 'calc app'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({super.key, required this.title});

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}
//演算子の型
enum CALC_TYPE { add, sub, multi, div }

class _MyHomePageState extends State<MyHomePage> {


  double _setNumber = 0;//数字ボタン押されたときに受け取る変数
  double _displayNumber = 0;//表示させる変数
  double _firstNum = 0;//最初に押された数値を裏で保持しとく変数
  CALC_TYPE? _calcType;//演算子を四つ格納された変数
  int _displayPow = 0;//桁は触れしたとき平方根を表示する
  bool _decimalFlag = false;

//数字のボタン押されたときに動くやつ
  void _setNum(double num) {
    _displayPow = 0;
   
    if (_displayNumber == _setNumber) {
      if (1000000000 > _displayNumber) {
        setState(() {
          if (!_decimalFlag)
            _displayNumber = _displayNumber * 10 + num;
          else {
            int count = 1;
            for (int i = 0;
                _displayNumber * Math.pow(10, i) !=
                    (_displayNumber * Math.pow(10, i)).ceil();
                i++) {
              count++;
            }
            _displayNumber = double.parse(
                (_displayNumber + (num / Math.pow(10, count)))
                    .toStringAsFixed(count));
            _checkDecimal();
          }
          _setNumber = _displayNumber;
        });
      }
    } else {
      setState(() {
        _displayNumber = num;
        _setNumber = _displayNumber;
        _calcType = null;
      });
    }
  }

//演算子が押された後の画面と裏で保持させる命令
  void _calcBtnPressed(CALC_TYPE type) {
    _setNumber = _displayNumber;//押された数字を表示
    _firstNum = _setNumber;//最初に入力した数値を保持
    _setNumber = 0;//二番目に入力する数値をいったん0
    _displayNumber = 0;//二番目に入力する前に0と表示
    _calcType = type;//
    _decimalFlag = false;

  }
//足し算の処理
  void _calcAdd() {
    setState(() {
      _displayNumber = _firstNum + _setNumber;
      _checkDecimal();
      _firstNum = _displayNumber;
    });
  }
  //引き算の処理
  void _calcSub() {
    setState(() {
      _displayNumber = _firstNum - _setNumber;
      _checkDecimal();
      _firstNum = _displayNumber;
    });
  }
  //掛け算の処理
  void _calcMulti() {
    setState(() {
      _displayNumber = _firstNum * _setNumber;
      _checkDecimal();
      _firstNum = _displayNumber;
    });
  }
//割り算の処理
  void _calcDiv() {
    setState(() {
      if (_displayNumber == 0 && _firstNum == 0 && _setNumber == 0) {
       _displayNumber = (double.parse('Infinity'));
       _firstNum = (double.parse('Infinity'));
       _setNumber = (double.parse('Infinity'));

      } else {
        _displayNumber = _firstNum / _setNumber;
      }

      _checkDecimal();
      _firstNum = _displayNumber;
    });
  }

  void _invertedNum() {
    setState(() {
      _displayNumber = -_displayNumber;
      _setNumber = -_setNumber;
    });
  }
//桁あふれ対応
  void _checkDecimal() {
    double checkNum = _displayNumber;
    if (1000000000 < _displayNumber ||
        _displayNumber == _displayNumber.toInt()) {
      for (int i = 0; 1000000000 < _displayNumber / Math.pow(10, i); i++) {
        _displayPow = i;
        checkNum = checkNum / 10;
      }
      //小数点は切り捨て
      _displayNumber = checkNum.floor().toDouble();
    } else {
      int count = 0;
      for (int i = 0; 1 < _displayNumber / Math.pow(10, i); i++) {
        count = i;
      }
      int displayCount = 9 - count;
      _displayNumber =
          double.parse(_displayNumber.toStringAsFixed(displayCount));
    }
  }
//クリアした時の処理
  void _clearNum() {
    setState(() {
      _setNumber = 0;
      _displayNumber = 0;
      _firstNum = 0;
      _calcType = null;
      _displayPow = 0;
      _decimalFlag = false;
    });
  }

  void _clearEntryNum() {
    setState(() {
      _setNumber = 0;
      _displayNumber = 0;
      _displayPow = 0;
      _decimalFlag = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),





      //ボタンの領域============================================================================
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: <Widget>[
          Container(
            height: 20,
            child: _displayPow > 0
                ? Text(
                 
                    "10^${_displayPow.toString()}",
                    style: TextStyle(
                      fontSize: 20,
                    ),
                  )
                : Container(),
          ),
          Text( //_displayNumberを表示させる
            _displayNumber == _displayNumber.toInt()//例1.0と1は等しいですか
                ? _displayNumber.toInt().toString()
                : _displayNumber.toString(),

                
            style: TextStyle(
              fontSize: 60,
            ),
          ),
          Expanded(
            child: Column(
              children: <Widget>[
                Expanded(
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _clearEntryNum();
                            },
                            child: Text(
                              "btn",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _clearEntryNum();
                            },
                            child: Text(
                              "AC",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _clearNum();
                            },
                            child: Text(
                              "C",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _calcBtnPressed(CALC_TYPE.div);
                            },
                            child: Text(
                              "÷",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _setNum(7);
                            },
                            child: Text(
                              "7",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _setNum(8);
                            },
                            child: Text(
                              "8",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _setNum(9);
                            },
                            child: Text(
                              "9",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _calcBtnPressed(CALC_TYPE.multi);
                            },
                            child: Text(
                              "×",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _setNum(4);
                            },
                            child: Text(
                              "4",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _setNum(5);
                            },
                            child: Text(
                              "5",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _setNum(6);
                            },
                            child: Text(
                              "6",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _calcBtnPressed(CALC_TYPE.sub);
                            },
                            child: Text(
                              "-",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _setNum(1);
                            },
                            child: Text(
                              "1",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _setNum(2);
                            },
                            child: Text(
                              "2",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _setNum(3);
                            },
                            child: Text(
                              "3",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _calcBtnPressed(CALC_TYPE.add);
                            },
                            child: Text(
                              "+",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _invertedNum();
                            },
                            child: Text(
                              "+/-",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _setNum(0);
                            },
                            child: Text(
                              "0",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              _decimalFlag = true;
                            },
                            child: Text(
                              ".",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              //押された演算子を判別させ、その処理を条件によって分ける
                              switch (_calcType) {
                                case CALC_TYPE.add:
                                  _calcAdd();
                                    
                                  break;
                                case CALC_TYPE.sub:
                                  _calcSub();
                                  break;
                                case CALC_TYPE.multi:
                                  _calcMulti();
                                  break;
                                case CALC_TYPE.div:
                                  _calcDiv();
                                  break;
                                default:
                                  break;
                              }
                            },
                            child: Text(
                              "=",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 40,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
      //ボタンの領域==================================================================================================
    );
  }
}


0 likes

No Answers yet.

Your answer might help someone💌