Solved

how the heck can i make this accept decimals ?

Posted on 2004-09-21
46
6,752 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
  • 19
  • 10
  • 7
  • +4
46 Comments
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 50 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 200 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
 
LVL 1

Author Comment

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

Assisted Solution

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

Assisted Solution

by:Kaarthick
Kaarthick earned 50 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 100 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 100 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 200 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 5

Expert Comment

by:darksinclair
ID: 12112690
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 100 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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now