Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 347 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Allow only two decimal number in flutter input?

#11
I think this is the most simplest and only way working for me:

`inputFormatters: [ LengthLimitingTextInputFormatter(2), ]`
Reply

#12
For Dart 2.00+

```dart
TextFormField(
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: [
// Allow Decimal Number With Precision of 2 Only
FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,2}')),
],
```

It allows decimals like `1.`, `.89`, `8.99`
Reply

#13
For **TextFeild in Flutter** with **Before decimal & After decimal length validation** class.


import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'dart:math' as math;

class DecimalTextInputFormatter extends TextInputFormatter {
DecimalTextInputFormatter({this.decimalRange,this.beforeDecimalRange})
: assert(decimalRange == null || decimalRange > 0 || beforeDecimalRange == null || beforeDecimalRange > 0 );

final int decimalRange;
final int beforeDecimalRange;

@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, // unused.
TextEditingValue newValue,
) {
TextSelection newSelection = newValue.selection;
String truncated = newValue.text;

String value;

if(beforeDecimalRange != null){
value = newValue.text;

if(value.contains(".")){
if(value.split(".")[0].length > beforeDecimalRange){
truncated = oldValue.text;
newSelection = oldValue.selection;
}
}else{
if(value.length > beforeDecimalRange){
truncated = oldValue.text;
newSelection = oldValue.selection;
}
}
}

if (decimalRange != null) {
value = newValue.text;

if (value.contains(".") &&
value.substring(value.indexOf(".") + 1).length > decimalRange) {
truncated = oldValue.text;
newSelection = oldValue.selection;
} else if (value == ".") {
truncated = "0.";

newSelection = newValue.selection.copyWith(
baseOffset: math.min(truncated.length, truncated.length + 1),
extentOffset: math.min(truncated.length, truncated.length + 1),
);
}

return TextEditingValue(
text: truncated,
selection: newSelection,
composing: TextRange.empty,
);
}
return newValue;
}
}


In TextFeild ,
Ex. Before decimal 9 digit & after Decimal 2 digit allowed code below.

inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d{0,2}')),
DecimalTextInputFormatter(decimalRange: 2, beforeDecimalRange: 9)
],
keyboardType: TextInputType.numberWithOptions(decimal: true),
Reply

#14
Dart SDK version: 2.13.4 (stable)
Flutter 2.2.3 • channel stable

i'm writing this answer on July 25, 2021, suggesting a little bit simpler solution, only using built-in TextField's `inputFormatters`.

i'm trying to ensure all of you, that the field won't accept floating point longer than 2(accepted: 12.25 vs notAccepted: 65.536). And also, it won't accept multiple dots, only accept a single dot(accepted: 12.25 vs notAccepted: 1.1.11, 1.11, .1.1, 1.1.1.1, whatsoever..).

The difference considering the other answers is that the code below won't accept `.1` which is equal to `0.1` programmatically, which is actually way more user-friendly. And it is simple, nice to look at for us. You can just copy and paste the code below into `inputFormatters: []`.

If you want to accept both `0.1` and `.1`(not only `0.1`), you can just comment out `FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d*'))`!

`inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')),
FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d*')),
]`

When you want to change the maximum decimal point, you can change the `N` value in the line below:

`inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,N}')),
FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d*')),
]`

First example shows the case in which you want to limit decimal point to 2, and the N value exactly controls the limit. You might try within your own code [:

Appreciate all the other answers, hope this answer might help future wanderers! Have a wonderful day [:
Reply

#15
This worked for me **(Feb/2022)**

**Restricts the text to 2 decimal point and have only 1 '.' (period)**

*0 will be automatically added to the start of the number if user go and manually remove the digit before the '.'*

*PS: A Slight edit to the answer*

**TextFormField**
```
TextFormField(
autofocus: false,
keyboardType: TextInputType.number,
controller: controller,
inputFormatters: [
DecimalTextInputFormatter(decimalRange: 2),
],
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please add amount.';
}
return null;
},
```

**DecimalTextInputFormatter**
```
import 'package:flutter/services.dart';
import 'dart:math' as math;

class DecimalTextInputFormatter extends TextInputFormatter {
DecimalTextInputFormatter({required this.decimalRange})
: assert(decimalRange > 0);

final int decimalRange;

@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue,
TextEditingValue newValue,
) {
TextSelection newSelection = newValue.selection;
String truncated = newValue.text;

String value = newValue.text;

if (value.contains(".") &&
value.substring(value.indexOf(".") + 1).length > decimalRange) {
truncated = oldValue.text;
newSelection = oldValue.selection;
} else if (value == ".") {
truncated = "0.";

newSelection = newValue.selection.copyWith(
baseOffset: math.min(truncated.length, truncated.length + 1),
extentOffset: math.min(truncated.length, truncated.length + 1),
);
} else if (value.contains(".")) {
String tempValue = value.substring(value.indexOf(".") + 1);
if (tempValue.contains(".")) {
truncated = oldValue.text;
newSelection = oldValue.selection;
}
if (value.indexOf(".") == 0) {
truncated = "0" + truncated;
newSelection = newValue.selection.copyWith(
baseOffset: math.min(truncated.length, truncated.length + 1),
extentOffset: math.min(truncated.length, truncated.length + 1),
);
}
}
if (value.contains(" ") || value.contains("-")) {
truncated = oldValue.text;
newSelection = oldValue.selection;
}

return TextEditingValue(
text: truncated,
selection: newSelection,
composing: TextRange.empty,
);
}
}
```


Reply

#16
If you are looking for a regexp to handle comma and dot with 2 decimals try this version:


TextFormField(
keyboardType: const TextInputType.numberWithOptions(decimal: true),
decoration: textFieldDecoration("Price with only 2 decimals and handling comma"),
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'(^\d*[\.\,]?\d{0,2})')),
],
);
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through