DEV Community

loading...

Format double according to locale

mohitkyadav profile image Mohit Kumar Yadav ・1 min read

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.

Discussion (0)

pic
Editor guide