Is there a special backbone-Way to format numbers? I want to display them with two decimal points, like "2" into "2.00".
Should this be done in the tepmlate?
This is one example from the template:
<td><input value="<%- price %>"></td>
In Angular.js you can use expressions and filters to achieve this, how is this in backbone?
Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.
js (aka Backbone) is designed to add structure to client-side applications, to avoid a spaghetti-code mess.
Use the built in toFixed method.
<td><input value="<%- price.toFixed(2) %>"></td>
Backbone is an MVC library, so it's emphasis is on the MVC components of the application. Formatting is really something you'd want to look at another library for.
One option would be the Underscore.String library. If you're using Backbone, you're also using Underscore, because Backbone requires it. Underscore.String is an sub-library for Underscore which adds a number of formatting functions, including a number formatting one that you can use like so:
_.numberFormat(1000, 2) // == "1,000.00"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With