DEV Community

Cover image for How you turn a string into a number (or vice versa) with Dart
Kelvin Wangonya
Kelvin Wangonya

Posted on

Dart String to Int How you turn a string into a number (or vice versa) with Dart

When you're so used to one language and you're trying to learn a new one, its likely that you'll find yourself always looking back to what you already know to find out if the new language matches up. I know I do. If I find similarities, I'm happy ๐Ÿ™‚. If I find more features, I'm excited ๐Ÿ˜„. If the new language is lacking, I'm like, how come something so important (however trivial the thing may be) is missing ๐Ÿคจ I can't work with this.

Say I needed to turn a string into a number with Javascript, there's a number of ways I could do it:

Dart has almost similar functions to achieve the same results.

Turning a string into a number (or vice versa) with Dart

You can try out the code snippets on dartpad

String to int

// String -> int
main () {
    var one = int.parse('1');
    print(one == 1); // prints true
}
Enter fullscreen mode Exit fullscreen mode

String to double

// String -> double
main () {
    var onePointOne = double.parse('1.1');
    print(onePointOne == 1.1); // prints true
}
Enter fullscreen mode Exit fullscreen mode

int to String

// int -> String
main () {
    String oneAsString = 1.toString();
    print(oneAsString == '1'); // prints true
}
Enter fullscreen mode Exit fullscreen mode

double to String

// double -> String
main () {
    String piAsString = 3.14159.toStringAsFixed(2);
    print(piAsString == '3.14'); // prints true
}
Enter fullscreen mode Exit fullscreen mode

I've been wanting to take up mobile app development with flutter so I thought I'd get comfortable with dart first before diving into flutter. So far, I'm happy. Coming from Javascript, Dart is easy to pick up, and fun to learn.

Top comments (16)

Collapse
 
creativ_bracket profile image
Jermaine

You just made my day bro! Happy to see another person covering raw dart concepts. Keep it up!

Collapse
 
wangonya profile image
Kelvin Wangonya

Hey Jermaine. Interesting that I did a search for Dart posts on Dev.to before posting this and I only saw your posts ๐Ÿ˜… Like almost all the search results were yours. I think Dart is really great and it isn't getting the attention it deserves. Thanks for stopping by!

Collapse
 
ben profile image
Ben Halpern

When I glanced at this title I figured it would be one of Jermaine's. ๐Ÿ˜„

Thread Thread
 
creativ_bracket profile image
Jermaine

๐Ÿ˜„

Collapse
 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€ • Edited

What's it like to work with Dart as JavaScript?

Collapse
 
wangonya profile image
Kelvin Wangonya

Hi Adam. To be honest, I just started out with Dart today so I can't give a very comprehensive answer. I think Jermaine's answer covers it though ๐Ÿ™‚

Collapse
 
creativ_bracket profile image
Jermaine

For me, working with Dart is a lot linear, since the necessary tooling are provided by the Dart team. Because it's a typed language, being productive in little time is easier, especially when combined with the range of utilities that come as part of the SDK. I can also use JavaScript libraries in my Dart web apps.

Collapse
 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€

Thanks for the reply, so I work with Typescript 99% of the time, from experience, I need to get a definition file in order the use them in TS in a meaningful way. How does this compare?

Is there a reason to consider Dart over TS? What is the coverage for the JavaScript library ecosystem in pub, have you had to make decisions based on what is in the pub repo.

Sorry to go off topic here @Kinyanjui.

Thread Thread
 
creativ_bracket profile image
Jermaine

from experience, I need to get a definition file in order the use them in TS in a meaningful way. How does this compare?

Similarly, a definition file is needed to use JS libraries in Dart. You can create one yourself with little effort. Furthermore, you can use this tool developed by the Dart team to convert TS definitions to Dart.

Is there a reason to consider Dart over TS?

Dart and TypeScript are both attempts to address the shortcomings of native JavaScript. I would consider Dart especially in the area of mobile app development with Flutter. Dart can be compiled to native ARM code, which is good for performance.

The pull with Dart here is the tooling and utilities that come with it, not just the language. I find with TypeScript that one can fall into the trap of writing code that looks too much like C# than JS, which tells me Dart may have been the right approach, implementing conventions that programmers use and love without being bound to the constraints of JavaScript.

What is the coverage for the JavaScript library ecosystem in pub, have you had to make decisions based on what is in the pub repo.

Haven't got any numbers on that one I'm afraid. There are Dart packages that act as wrappers over JS libs, using the js interop package I covered in my tutorial. I found examples like Google Maps and ChartJS, although you can make your own using the tools provided.

Thanks for your questions. I'm up for suggestions on any future topics you wish for me to cover. Message me on Twitter ๐Ÿฆ.

Collapse
 
wangonya profile image
Kelvin Wangonya

I didn't know you can use Javascript libraries in Dart. That's really cool.

Thread Thread
 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€

Before Dart as it is today, Dart was going to be a replacement for JavaScript (in Googles mind), There is a Dartium version of chrome that ran Dart 1. The decision was made to rebirth Dart into what it is today, but I see that the JavaScript compiler exists. So the idea is not dead, just different.

Thread Thread
 
creativ_bracket profile image
Jermaine

The dart2js compiler to JavaScript is necessary if we need to develop apps that run on the web. An aim with Dart 2 was to improve the tooling geared towards clientside development. They wrote about this on their Medium blog.

Collapse
 
nisadrahman profile image
Foyez A.

You won't be able to check if a number is lesser or greater than other by following this way

Collapse
 
codewithlennylen profile image
Lenny Ng'ang'a

Thanks a lot, I am a python dev learning Flutter ๐Ÿ˜…

Collapse
 
splez profile image
splez • Edited

This doesn't work, int.phase and int.tryPhase are broken and return null nomatter what. please fix dart.

Collapse
 
souviktests profile image
Souvik Paul

Thank you so much, brother ๐Ÿ˜. I just came back from a nightmare just because of you. Much love โค๏ธ