Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Backbone-way to format numbers in the view?

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?

like image 694
FuzzBuzz Avatar asked Apr 21 '13 00:04

FuzzBuzz


People also ask

Is Backbone JS still used?

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.

Which is considered the backbone of any HTML document?

js (aka Backbone) is designed to add structure to client-side applications, to avoid a spaghetti-code mess.


2 Answers

Use the built in toFixed method.

<td><input value="<%- price.toFixed(2) %>"></td>
like image 59
pgerstoft Avatar answered Sep 20 '22 03:09

pgerstoft


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"
like image 31
machineghost Avatar answered Sep 21 '22 03:09

machineghost