Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6812293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:25:54+00:00 2026-05-26T20:25:54+00:00

I am using KnockoutJS and I have an input control that displays a number

  • 0

I am using KnockoutJS and I have an input control that displays a number from my model.

Everything works fine, however I’d like to format my number before displaying it in the input control – but I want to keep it as a number to my view! So, let’s say that I have the following code:

<td><input class='requiredNum' type='text' data-bind="value: testNum"></input></td>

I have a function toText() that converts the number to what I need. However, this doesn’t work:

<td><input class='requiredNum' type='text' data-bind="value: toText(testNum)"></input></td>

I can understand that the problem is that Knockoutjs doesn’t know how to do the opposite (convert from text to number) – however this is not a problem to me because I already handle that in another way.

Can you recommend me a solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:25:55+00:00Added an answer on May 26, 2026 at 8:25 pm

    The easiest solution is to use a writeable dependentObservable.

    var viewModel = {
        number: ko.observable(10)
    };
    
    viewModel.formattedNumber = ko.dependentObservable({
      read: function() {
         //do formatting on this.number() here
         return this.number().toFixed(4);
      },
      write: function(newValue) {
         //take user input and parse into a number
         this.number(parseFloat(newValue));
      },
      owner: viewModel
    });
    

    Sample:
    http://jsfiddle.net/rniemeyer/8bsAF/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From what I have done in past(before using knockoutjs) I would call the tabs()
I'm using KnockoutJS and have a main view and view model. I want a
Using online interfaces to a version control system is a nice way to have
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
I'm using KnockoutJS and MVVM in a page and most if it works, but
I'm using Knockout with jQuery and jQuery templates. Assume that I have a template
I have a radio group that allows the user (using jQuery events) to deselect
I'm using CoffeeScript and KnockoutJS and have a problem getting the values of my
I'm using knockoutjs 2.0 I've been trying to have this table work in IE8
I have setup Knockoutjs to dynamically create an editable list of values using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.