We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Is there a way to format decimal places in a Textbox with a number in it?

MitchellVII
MitchellVII asked
on
Medium Priority
1,330 Views
Last Modified: 2011-09-20
Hi,

I am putting a textbox on my form and would like it to automatically show a format of 0.0.

For instance, if the person just enters a 12, the texbox should change to 12.0 automatically.

Is this possible?

M
Comment
Watch Question

Irwin SantosComputer Integration Specialist

Commented:
not with straight HTML, you could incorporate javascript  OR

have an integer / decimal form setup (archaeic, but it works)
irwinpks is correct.  You need to use JavaScript to accomplish this.

You could use a few different methods, like onChange or onKeyDown to fire a Javascript function.  That function would then need to parse the text of the field, and format it...I won't write it for you, but here is a "rough" start.

<html>
<body>
<script>
function FormatNum(field)
{
  ... Logic to do the magic
}
</script>
<form name="theForm">
<input type="text" name="number" value="0.0" onkeydown="FormatNum(this)" />
</form>
</body>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
mmark,

Any idea on what that code that forces the decimal point would look like?
Hi,

You can use the following Javascript code to achieve that,

<HTML>
<HEAD>
 <script language="JavaScript">
   function formatnum(){
     var valu1 = parseInt(document.test.txt1.value);
     document.test.txt1.value = valu1.toFixed(1);
     }
 </script>
</HEAD>
<BODY BGCOLOR=#FFFFFF>
<form name="test">
  <input type="text" name="txt1" onchange="formatnum()"/>
</form>
</BODY>
</HTML>

Regards - RM.
initial value would be like

<input type="text" name="txt1" value="0.0" onchange="formatnum()"/>

Author

Commented:
thanks rm,

Is there a way I can add an argument to the function so that I can pass the text field name to it?  This needs to work in about 50 places.

M
<script language="JavaScript">
   function formatnum(obj){
      if ( !isNaN(obj.value) )
            if (parseFloat(obj.value).toFixed(2) != "NaN")
                  obj.value = parseFloat(obj.value).toFixed(2);
    }
</script>

<input type="text" name="txt1" value="" onchange="formatnum(this)"/>

Author

Commented:
Hey bubble,

would I actually type "this" in the argument or the name of the text field?

M
Yes, will return the textbox object to the function.

Author

Commented:
I bow before you superior knowledge oh great one - works like a charm :)

Mitchell

Author

Commented:
Oh yeah, also meant to ask you:

How do I format a field so that it returns a dollar amount?

So that if they entered 125000 it would show $125,000.00?

Thanks :)

M
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.