writeDouble function
Rounds a double for storage: integral values become ints, others keep up to 4 decimals (sub-pixel noise from editors goes away).
Implementation
num writeDouble(double v) {
final r = (v * 10000).roundToDouble() / 10000;
return r == r.roundToDouble() && r.abs() < 1e15 ? r.toInt() : r;
}