Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# how the heck can i make this accept decimals ?

Posted on 2004-09-21
Medium Priority
6,759 Views
e.value=parseInt(e.value.replace(/[^\d]*/gi,\"\"));

can anyone recommend a site with javascript reference where this information can be found ?
0
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
• 19
• 10
• 7
• +4

LVL 49

Assisted Solution

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

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

ID: 12112499
just a US dollar amount
0

LVL 1

Author Comment

ID: 12112502
second place decimals
0

LVL 5

Assisted Solution

darksinclair earned 800 total points
ID: 12112504
0

LVL 4

Assisted Solution

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

LVL 1

Author Comment

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

doesnt work
0

LVL 1

Author Comment

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

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

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

ID: 12112562
Sorry, remove the +500.

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

LVL 1

Author Comment

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

ID: 12112581
i'd like to stick with replace function
0

LVL 1

Author Comment

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

LVL 13

Assisted Solution

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;
0

LVL 5

Assisted Solution

darksinclair earned 800 total points
ID: 12112626
<SCRIPT>
var e = 2.3456;
e.value=parseFloat(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

ID: 12112653
e=e.toFixed(2)
is another option
0

LVL 1

Author Comment

ID: 12112672
what does to.fixed do ?
0

LVL 5

Expert Comment

ID: 12112690
0

LVL 1

Author Comment

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

ID: 12112720
var the_float = parseFloat(document.form_name.textbox_name.value);
0

LVL 5

Expert Comment

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

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

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

LVL 1

Author Comment

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

ID: 12112772
i have to step out brb in an hr
0

LVL 33

Expert Comment

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

ID: 12112912
<SCRIPT>
function doE()
{
var e = document.getElementById( "e" ).value;
e = parseFloat(e);
e = e.toFixed(2);
}
</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

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

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

LVL 33

Expert Comment

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

LVL 1

Author Comment

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

LVL 1

Author Comment

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

LVL 33

Expert Comment

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

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

LVL 33

Accepted Solution

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

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

ID: 12114923
it does!  :)
0

LVL 1

Author Comment

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

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

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

LVL 75

Expert Comment

ID: 12118131
PS: I posted toFixed(2) not tofixed(2)  JS is case sensitive
0

LVL 33

Expert Comment

ID: 12122118
what object does the toFixed method operate on?
0

LVL 75

Expert Comment

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

ID: 12122701
thanks, I didn't know about that ...
0

LVL 1

Author Comment

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

ID: 12123409
Sorry I do not understand the question
0

LVL 33

Expert Comment

ID: 12123757

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

e=e.toFixed(2)
0

## Featured Post

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some ofâ€¦
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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
Course of the Month9 days, 13 hours left to enroll