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

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
LVL 5
MitchellVIIAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Irwin SantosComputer Integration SpecialistCommented:
not with straight HTML, you could incorporate javascript  OR

have an integer / decimal form setup (archaeic, but it works)
mmarksburyCommented:
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>
MitchellVIIAuthor Commented:
mmark,

Any idea on what that code that forces the decimal point would look like?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

r_m_shyamCommented:
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.
r_m_shyamCommented:
initial value would be like

<input type="text" name="txt1" value="0.0" onchange="formatnum()"/>
MitchellVIIAuthor 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
bubbledragonCommented:
<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)"/>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MitchellVIIAuthor Commented:
Hey bubble,

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

M
bubbledragonCommented:
Yes, will return the textbox object to the function.
MitchellVIIAuthor Commented:
I bow before you superior knowledge oh great one - works like a charm :)

Mitchell
MitchellVIIAuthor 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.