DEV Community

Mohit Kumar Yadav
Mohit Kumar Yadav

Posted on

Format double according to locale

Problem

I was working on an app targeted for Germany. And in German locale 12.5 is written 12,5, 1.050 in German means one thousand and fifty.

Solution

I created 2 utility functions 1st to parse double to de locale and second to convert back to double.

Here is the code

import 'package:intl/intl.dart';

static String deFormat (String val) {
    final deFormat = NumberFormat.decimalPattern('de',);
    return deFormat.format(double.parse(val));
  }


  static num enFormat (String val) {
    final deFormat = NumberFormat.decimalPattern('de',);
    return deFormat.parse(val);
  }

Enter fullscreen mode Exit fullscreen mode

You'll need intl package.

Top comments (0)