This is a migrated thread and some comments may be shown as answers.

Format output?

4 Answers 249 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 23 Mar 2012, 03:51 PM
Hello,

I have some values being returned from a remote data source and would like them to be formatted as currency.  So currently I have something like:

<span data-bind="value: amount"></span>

And it's outputting something like 59.9 and I would like it to output $59.90.

Is there a recommended way to accomplish this without hard-coding the $ and changing the data type to a string?

  Nick

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 23 Mar 2012, 04:04 PM
Hi,

 You need to create a function which returns the formatted data:

var viewModel = kendo.observable({
amount: 59.90,
        amountCurrency: function() {
              return "$" + this.get("amount");
        }
});

<span data-bind="text: amount"></span> 

Note that the value binding does not work with span elements. You need to use the text binding instead. 

Kind regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Jeff
Top achievements
Rank 1
answered on 23 Mar 2012, 06:26 PM
Good catch on the text/value - still trying to get those to stick :)

So writing a separate viewmodel function whenever I need to format a value will add a lot of extra code.  Do you know if there are plans for a more elegant solution?  Either being able to specify the format in the HTML tag itself, through the data model, etc?
0
Atanas Korchev
Telerik team
answered on 24 Mar 2012, 08:08 AM
Hi,

 You may consider opening a new item in our user voice page and submit this as a feature request. I guess the best API would be a new data- attribute:

<span data-format="{0:C}" data-bind="text: amount"></span>

Kind regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Vladimir
Top achievements
Rank 1
answered on 05 May 2012, 09:31 AM
Is there a way at present to specify a two-way data binding with formatting?
Tags
MVVM
Asked by
Jeff
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Jeff
Top achievements
Rank 1
Vladimir
Top achievements
Rank 1
Share this question
or