Using Closures Instead of Classes

Learn when to use closures instead of classes with the help of an example.

Number formatter

For the final example, we will look at a simple number formatter. We want a formatter that can convert a floating-point number to a string, with a fixed number of decimal places.

Using a class

We could do this with a simple class, as shown below. The Format() class can be used to create a format object (as with the three digit case on line 10), or you can create and call the object in one statement (as with the five digit case shown on line 13):

Get hands-on with 1400+ tech skills courses.