Convert long double to string without scientific notation (Dart)
Asked Answered
H

3

7

I want to convert a long double to string without scientific notation in Dart, I thought just doing a .toString() would do the trick but it's not the case.

Here's my code:

void main() {  
  double num = 0.00000000196620214137477;
  String numToString = num.toString();
  
  print(num);
  print(numToString);
}

This is what it prints:

1.96620214137477e-9
1.96620214137477e-9

I want a string representation of the number as it was written when defined. So the printout I want after converting to string is:

0.00000000196620214137477

Thanks in advance.

Hupp answered 20/7, 2020 at 6:19 Comment(2)
If the string representation of the number was 0.00000196620214137477e-3, would you want to see that as the output too?Anathema
There is another way to do the same using decimal package.Indian
C
8

There is no easy way to do what you want directly with the Dart platform libraries, mainly because there is no way to do it in JavaScript.

You can use num.toStringAsFixed(20), but that is limited to 20 digits, and your number needs 23.

One thing you could do is to manipulate the string directly:

String toExact(double value) {
  var sign = "";
  if (value < 0) {
    value = -value; 
    sign = "-";
  }
  var string = value.toString();
  var e = string.lastIndexOf('e');
  if (e < 0) return "$sign$string";
  assert(string.indexOf('.') == 1);
  var offset = int.parse(string.substring(e + (string.startsWith('-', e + 1) ? 1 : 2)));
  var digits = string.substring(0, 1) + string.substring(2, e);
  if (offset < 0) { 
    return "${sign}0.${"0" * ~offset}$digits";
  }
  if (offset > 0) {
    if (offset >= digits.length) return sign + digits.padRight(offset + 1, "0");
    return "$sign${digits.substring(0, offset + 1)}"
        ".${digits.substring(offset + 1)}";
  }
  return digits;
}

Do notice that you can't necessarily get the value as written because not all double values are exact. In this particular case, you seem to be be fine.

Chalutz answered 20/7, 2020 at 6:58 Comment(4)
Sir could the formatting be achieved using NumberFormat ?Indian
Probably, yes. The intl package's NumberFormat seems to be doing roughly the same thing for fractions - convert the fraction to a string, then manipulate the string.Chalutz
Sir there is another way to do the same using decimal package.Indian
True. You'd have to go through the exponent notation, as Decimal.parse(myDouble.toString()), but then it should probably work.Chalutz
I
5

There is another simpler way to do this using decimal package. Recently came across flutter-dart-number-handling-ability question which pointed towards the package.

Example :

double originalNum = 0.00000000196620214137477;
Decimal convertedNum = Decimal.parse(originalNum.toString());
print('originalNum: $originalNum \n convertedNum: $convertedNum');

Output:

originalNum: 1.96620214137477e-9 
convertedNum: 0.00000000196620214137477
Indian answered 2/8, 2020 at 6:52 Comment(0)
E
0

It's anoyying that Flutter toString method does not do the job If It's over 20 digits, I found much more easier solution:

String formatLargeNumber(double number) {
  NumberFormat formatter = NumberFormat('###', 'en_US');
  return formatter.format(number);
}

Elegist answered 22/2 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.