0

I want to achieve the following in my code, and can't seem to find a proper solution: I need code that will always show 6 digits of a number, no matter being int greater than 999999 or floating point smaller than 0.

100000 -> 100000
1000000 -> 100000

10000.0 -> 10000
100.01111 -> 100.011

0.000001 -> 0
0.000011 -> 0.00001

With some help in the comments, I got a solution that works for me. If someone has more elegant way of doing this please do share it.

int desiredPrecision = 6;
int numberOfDigitsOnTheLeft = val.toInt().toString().length;
String sixDigitString = val.toStringAsFixed(desiredPrecision-numberOfDigitsOnTheLeft);
Aleksandar
  • 1,457
  • 13
  • 30
  • see [num](https://api.flutter.dev/flutter/dart-core/num-class.html) `toString*` methods – pskink Sep 04 '19 at 08:52
  • @pskink num.toString methods don't provide the appropriate solution to my specific problem. They give either precise number of decimals or appropriate number of significant decimals. – Aleksandar Sep 04 '19 at 08:56
  • @Eugene all answers from "duplicate" show how to set specific number of decimals, but not solution on how to set specific number of digits. Different kind of question – Aleksandar Sep 04 '19 at 08:58
  • find how many numbers is on the left side of the dot then (call it `left`) and on the right side you will need `5 - left` - for example if you have 2 numbers on the left then you will need 3 numbers on the right – pskink Sep 04 '19 at 08:59
  • edit: in your case its `6`, not `5` – pskink Sep 04 '19 at 09:06
  • Got it! Will do it that way, but I taught there might be more elegant way of achieving this. Thanks! :) – Aleksandar Sep 04 '19 at 09:08
  • `Math.log` is elegant way i think – pskink Sep 04 '19 at 09:08
  • https://stackoverflow.com/questions/28419255/how-do-you-round-a-double-in-dart-to-a-given-degree-of-precision-after-the-decim – Amit Prajapati Sep 04 '19 at 09:14
  • Just edited my question to provide my solution with the idea @pskink had. – Aleksandar Sep 04 '19 at 09:17
  • print(log(1) * log10e); print(log(9) * log10e); print(log(10) * log10e); print(log(99) * log10e); print(log(100) * log10e); print(log(999) * log10e); – pskink Sep 04 '19 at 09:18

1 Answers1

4

as an option

void main() {
    print(_normalizeNum(10000.0));
    print(_normalizeNum(100000));
    print(_normalizeNum(1000000));
    print(_normalizeNum(10000.0));
    print(_normalizeNum(100.01111));
    print(_normalizeNum(0.000001));
    print(_normalizeNum(0.000011));
}

String _normalizeNum(num d) {
  d = d.clamp(double.negativeInfinity, 999999);
  d = num.parse(d.toStringAsFixed(6).substring(0, 7));
  if (d == d.toInt()) {
    d = d.toInt();
  }
  return d.toString();
}
  • 1
    Out of curiosity, why is clamping not done on 999999, but on 100000 – Aleksandar Sep 04 '19 at 09:28
  • but wait, you wrote `1000000 -> 100000` so it should clamp to 100000 –  Sep 04 '19 at 09:31
  • Yea, but in case of 999999 it should not clamp to 100000. It should keep 6 digit representation of that number. Should be ok to keep it at max limit of overflow in case of greater numbers in this case. I'll keep 999999 as upper limit :) – Aleksandar Sep 04 '19 at 09:34