Sören
Sören

Reputation: 33

Format numbers in flutter/dart dynamically

I'm just figuring out how to format numbers. I use the NumberFormat class to transform a number in a currency with following

NumberFormat("#,##0.00€", 'de_de")

This works fine for value with "normal prices like 1,12€ or whatever. But I have value, that can be 0,000000001€ or something like this. Is there any way to dynamically (maybe with a parameter) to descrive, how many digits there have to be after the comma?

I would appreciate any help :)

Upvotes: 0

Views: 640

Answers (1)

Shahood ul Hassan
Shahood ul Hassan

Reputation: 799

You can achieve this by defining the number of required digits in a variable and then using string interpolation to define the number pattern.

For example:

int numDigits = 6;

NumberFormat('#,${'#' * numDigits}0.00€', 'de_de')

Hope it helps!

Upvotes: 2

Related Questions