?
Solved

how the heck can i make this accept decimals ?

Posted on 2004-09-21
46
Medium Priority
?
6,758 Views
Last Modified: 2008-01-09
e.value=parseInt(e.value.replace(/[^\d]*/gi,\"\"));

can anyone recommend a site with javascript reference where this information can be found ?
0
Comment
Question by:aot2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 19
  • 10
  • 7
  • +4
46 Comments
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 200 total points
ID: 12112478
does javascript support parseDouble, because parseInt as the functionname says parses integers, which by default are rounded numerics.

-r-
0
 
LVL 5

Assisted Solution

by:darksinclair
darksinclair earned 800 total points
ID: 12112490
how many decimal accuracy do you want?

you can use parseFloat( )

to get floating point precision.

Cheers.
0
 
LVL 1

Author Comment

by:aot2002
ID: 12112499
just a US dollar amount
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:aot2002
ID: 12112502
second place decimals
0
 
LVL 5

Assisted Solution

by:darksinclair
darksinclair earned 800 total points
ID: 12112504
0
 
LVL 4

Assisted Solution

by:Kaarthick
Kaarthick earned 200 total points
ID: 12112509
To accept decimals try parseFloat function
0
 
LVL 1

Author Comment

by:aot2002
ID: 12112524
e.value=parseFloat(e.value.replace(/[^\d]*/gi,\"\"));

doesnt work
0
 
LVL 1

Author Comment

by:aot2002
ID: 12112529
Comment from Kaarthick  feedback
Date: 09/21/2004 10:23AM EDT

No kidding ! i think i heard the first time
0
 
LVL 5

Expert Comment

by:darksinclair
ID: 12112547
aot2002, Whats the error and what are you trying to do with the regular expression?

Check the link that I gave for a working example of parseFloat() function
0
 
LVL 13

Assisted Solution

by:cLFlaVA
cLFlaVA earned 400 total points
ID: 12112554
var val = Math.round(((500/100) * 2.8375643 + 500 * 100) / 100);

The above should give val a value of 2.84.
0
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12112562
Sorry, remove the +500.

var val = Math.round((2.8375643 * 100) / 100);
0
 
LVL 1

Author Comment

by:aot2002
ID: 12112575
im erasing any characters automatically except decimals and numbers
im using the text field as a dollar range
0
 
LVL 1

Author Comment

by:aot2002
ID: 12112581
i'd like to stick with replace function
0
 
LVL 1

Author Comment

by:aot2002
ID: 12112587
Comment from cLFlaVA  feedback
i'd like to stick with replace function
0
 
LVL 13

Assisted Solution

by:cLFlaVA
cLFlaVA earned 400 total points
ID: 12112602
Ok, but let me just correct my code for anyone else who may read this:

var the_float_number = 234.7896578;
alert(Math.round(the_float_number * 100) / 100);
0
 
LVL 5

Assisted Solution

by:darksinclair
darksinclair earned 800 total points
ID: 12112626
<SCRIPT>
var e = 2.3456;
e.value=parseFloat(e);
alert( e );
</SCRIPT>

^^ that works.  The problem is with your Regular Expression.  It is giving an invalid character error when you try to run it with your replace.

I am assuming that e is also a text box since you are doing e.value aswell.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 12112653
e=e.toFixed(2)
is another option
0
 
LVL 1

Author Comment

by:aot2002
ID: 12112672
what does to.fixed do ?
0
 
LVL 1

Author Comment

by:aot2002
ID: 12112700
that didnt work nor the float or the e.tofixed

e.tofixed says object doesnt support this property
and float says in alert box NaN
0
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12112720
var the_float = parseFloat(document.form_name.textbox_name.value);
alert(the_float.toFixed(2));
0
 
LVL 5

Expert Comment

by:darksinclair
ID: 12112729
can you paste your code that you are working with.. You havent given us enough to know what your doing.. obviously your taking a text box in javascript and trying to do what with it?  What code do you have?  if your getting NaN with parseFloat() then you obviously still have characters in your text box which need to be removed.  (that's what your replace was trying to do but it has errors and will not work the way you want it too.

If you give us your code we may be able to help more. (complete code)
0
 
LVL 1

Author Comment

by:aot2002
ID: 12112742
what part of this expression is removing the decimal ????

e.value=parseFloat(e.value.replace(/[^\d\.]*/gi,\"\"));  
0
 
LVL 1

Author Comment

by:aot2002
ID: 12112754
im erasing any characters automatically except decimals and numbers
im using the text field as a dollar range

then on keyup i call this function





 function IsNumeric2(e)
{

        e.value=parseFloat(e.value.replace(/[^\d\.]*/gi,\"\"));

         if (isNaN(e.value) == true)
         e.value = '';

         e.focus();
       return false;




       }




0
 
LVL 1

Author Comment

by:aot2002
ID: 12112772
i have to step out brb in an hr
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12112848
e.value=parseFloat(e.value.replace(/[^\d\.]*/gi,\"\"));

... but that will allow more than one . in the original number
0
 
LVL 5

Expert Comment

by:darksinclair
ID: 12112912
<SCRIPT>
function doE()
{
      var e = document.getElementById( "e" ).value;
      e = parseFloat(e);
      alert( e );
      e = e.toFixed(2);
      alert( e );
}
</SCRIPT>
<FORM>
<INPUT TYPE="text" ID="e" name="e" value="2342.3343445">
<INPUT TYPE="button" value="do E!" onClick="doE();">
</FORM>

not sure what else to tell you here... the above works as long as the input is numeric,,, The problem is with your regular expression.  You need to fix that and get all teh characters out and then the above will do everything want.

Cheers.
0
 
LVL 1

Author Comment

by:aot2002
ID: 12114112
e.value=parseFloat(e.value.replace(/[^\d\.]*/gi,\"\"));

What part of this expression actually tells it not to allow DECIMALS ?
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12114764
it DOES allow decimals ... the problem is, it can potentially allow more than one :)
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12114777
... at least with respect to the replace() method -- but the parseFloat wrapper might take care of that.
0
 
LVL 1

Author Comment

by:aot2002
ID: 12114842
well i cant seem to get it to allow decimals on my end ?
0
 
LVL 1

Author Comment

by:aot2002
ID: 12114850
i dont even get an error just cannot put in any decimals ?
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12114859
sorry, there is a typo ... remove the backslashes:

e.value=parseFloat(e.value.replace(/[^\d\.]*/gi,""));
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 400 total points
ID: 12114882
for example:

  <INPUT type="text" name="numeric" onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d\.]*/gi,"");' />

this will not allow non-digit characters to be entered in the field.
0
 
LVL 1

Author Comment

by:aot2002
ID: 12114914
this will not allow non-digit characters to be entered in the field.

ok GREAT how about getting it to allow DECIMALS too ?
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12114923
it does!  :)
0
 
LVL 1

Author Comment

by:aot2002
ID: 12114991
you are correct but i still didnt understand why the function i called wouldnt allow decimals ?
ohh well
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12115005
i didn't look at the function that carefully, sorry.  But parseFloat should be able to handle that well enough -- but again, I didn't test it.
I hope that was helpful -- thanks for the A.
0
 
LVL 1

Author Comment

by:aot2002
ID: 12115014
you deserved it !
after i stressed for hrs on somehting so simple !
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 12118131
PS: I posted toFixed(2) not tofixed(2)  JS is case sensitive
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12122118
what object does the toFixed method operate on?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 12122571
number only
a=1.555
b="2.555"
document.write('<b>'+a.toFixed(2)+'<br>'+parseFloat(b).toFixed(2)+'</b>')
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12122701
thanks, I didn't know about that ...
0
 
LVL 1

Author Comment

by:aot2002
ID: 12123364
your right you did post to.Fixed
i missed typed there.

Anyway why would that say a object not supported on a textbox ?
or do you get this error on smaller case --> to.fixed ?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 12123409
Sorry I do not understand the question
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12123757
I think what mplungjan mean was this:

   e.value = (1*e.value).toFixed(2)

instead of:

   e=e.toFixed(2)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question