Help with displaying a decimal number

Hi, I’m working through the book and trying to apply what I’ve learnt creating the recipe app to my own ‘playground’ app.

I’ve created a basic currency conversion app, the slider adjusts an input amount and the return gives values for a particular currency. I’d like to limit the decimal places to 2.

Simulator Screen Shot - iPhone 11 - 2021-10-25 at 07.48.32

How would I adjust this code to limit the decimal places?

Text('${currencyFx.exRate * _sliderVal}',
                                style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.w300),),

Thanks in advance

I think you can use this method toStringAsFixed method - num class - dart:core library - Dart API

Thanks @funkyboy

I have tried the toStringAsFixed method, as you can see I get an error

Text(('${currencyFx.exRate * _sliderVal}').toStringAsFixed(3),

It’s likely I’m not implementing it correctly.
Any pointers on what I am doing wrong?

Text((currencyFx.exRate * _sliderVal).toStringAsFixed(3)),

That’s untested but should work.

If you want to dive deeper into Dart (the foundation of Flutter) you can check out our Dart Apprentice book here: https://www.raywenderlich.com/books/dart-apprentice

1 Like

Thanks, struggle as it is just following the Flutter Apprentice book. Think I need to get my head into the courses more. I guess I should review the flutter chapters again, I must have missed something.

Is it best to do the Dart Apprentice book before Flutter Apprentice, both at the same time or is it ok to go with Flutter Apprentice for now?

I stumbled late to the Flutter Apprentice course, came across it on the Flutter website and jumped right in and started watching all the videos, reading the book online, so I probably have missed something.

Would you say experimenting is not advised at the early stages, should I stick to following the book and just copy and understand the tutorials?

There is so much information out there, it’s hard to know which roadmap to follow.

1 Like

First, maybe this code is a bit easier to understand.

var value = (currencyFx.exRate * _sliderVal).toStringAsFixed(3)
Text(value)

As for the path, that depends on your current knowledge and learning style. If you are already familiar with other languages with a C-like syntax, Dart and Flutter will be easier to grasp.
But it’s also ok to experiment, ask questions along the way, and learn some of the foundations later.

It also depends on your goal. If you aim at becoming a professional Flutter dev, you’ll need to have a very good grasp of Dart sooner or later, so it probably makes sense to learn it sooner.

@carlstep333 … The ${…} turns whatever is within it to a String …

So you’ve turned the computation into a String. Then by saying .ToString, you’re telling it to turn the String into a String. That’s why @funkyboy 's code should work and your doesn’t.

Except I think you want it to be .toStringAsFixed(2)

Thank Jeff, I could figure out that .toStringAsFixed was the method, just couldn’t work out how to amend it into the existing code. the string interpolation $ was tripping me up. @funkyboy was very helpful.

Thanks for your input.

@carlstep333 regarding the start with Dart or Flutter, as @funkyboy mentioned it all depends on what your coding background is. For me personally, I have been a SQL developer for over 20 years. I do have some iOS development experience. When I was first introduced to Flutter it was a “Flutter path”. I learned Dart along the way.

That being said, I do recommend reading Dart Apprentice so that you have deeper understanding of Dart, but it is not required to learn Flutter.

@carlstep333 One more thing regarding this:

Hi, I’m working through the book and trying to apply what I’ve learnt creating the recipe app to my own ‘playground’ app.

I’ve created a basic currency conversion app, the slider adjusts an input amount and the return gives values for a particular currency.

I love that you’re creating a different app! This will help you learn how to adapt what we’ve taught you in Flutter Apprentice.

I’d love to see your final project if you’re willing to share. I’m part of the team leading the Flutter Apprentice Book Club and we’re interested to see what people have created. If you’re willing, please share here and also tag me on Twitter at @GeekMeSpeakStef.

1 Like