Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1876
  • Last Modified:

How javascript truncate to have maximum 6 decimal places?

Hi

I need to limit the input decimal places to be 6 or less in javascript, how can i do that?

if(isEmpty(strCounter))  // how to limit the input to be 6 decimal places?

e.g.
13000.712 - ok
145221.987234 - ok
528132.097123124 - false

I use ASP.NET

Thanks

function CheckInput()
{
var objRec = xmlindata.recordset;
var objTR;
 var strCounter;
var count = 0;
for( var i=1; i<=objRec.RecordCount; i++ )
{
objRec.AbsolutePosition	 = i;
strCounter	  = objRec.Fields("countervalue").value;
 
 if(xmlindata.recordset.RecordCount>1)
{
objTR = idDefView.all.DataRow.item(i-1);
}
else
{
objTR = idDefView.all.DataRow;
}
  if(isEmpty(strCounter))  // how to limit the input to be 6 decimal places?
{
window.alert("Pls input Counter Value");
objTR.all.countervalue.focus();
return false;
}	
count++;
 }		
}

Open in new window

0
techques
Asked:
techques
  • 5
  • 3
  • 3
  • +1
1 Solution
 
brandonvmooreCommented:
There are various ways to accomplish this and I imagine there is a more correct way than what I'm offering, but here are a few possibilities:

Multiply the number by 1000000 (promote 6 digits fractional digits to be whole numbers)
Cast as an integer (gets rid of any remaining fractional digits)
Divide by 1000000 (demote those 6 digits back to their original place)

Another approach would be to cast it as a string and look for the decimal point.

A more interactive solution would be to actively watch their input and watch for when they hit the decimal and count keystrokes.  This isn't a very good solution as they can do a lot of things with the input that would mess your code up if you don't handle it (ie. if they pasted a number into the field the keystroke values would only indicate a Ctl-V, or nothing if they did it with the mouse).
0
 
techquesAuthor Commented:
then how to change the script?
0
 
brandonvmooreCommented:
Well I see three ways that you could get your script changed.

1.  Learn how to program in Javascript.  There is a multitude of information on the internet covering this topic.
2.  Hire someone to do it.
3.  Patiently wait until some kind soul with nothing better to do (there are plenty of them) does your homework for you.

Good luck ;)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rockiroadsCommented:
Look at using regular expressions - more info here http://lawrence.ecorp.net/inet/samples/regexp-validate2.php

0
 
techquesAuthor Commented:
I tired var regxp = /^[-+]?\d{6,10}(\.\d{1,6})?$/;

but it returns false for 1234567890.123456

why?
0
 
HonorGodCommented:
Was there anything else in the string?
For example, ' 1234567890.123456' would fail because of the leading blank
and '1234567890.123456 ' would fail because of a trialing one.
0
 
HonorGodCommented:
Using the following test produced this (annotated) output:
--------------------------------------------------------------------
1234.5678                       false - Not enough leading digits
123456789.123456789      false - Too many trailing digits
1234567890.123456      true  - Just right
13000.712                       false - Not enough leading digits (5, not 6)
145221.987234               true  - 6 ahead, and 6 behind
528132.097123124       false - 6 ahead, and 9 behind (too many)

<html>
<body>
 
<script type="text/javascript">
 
var values = '1234.5678,123456789.123456789,1234567890.123456,13000.712,145221.987234,528132.097123124'.split( ',' )
 
for ( var i = 0; i < values.length; i++ ) {
  var val = values[ i ]
  document.write( val + '\t' + ( /^[-+]?\d{6,10}(\.\d{1,6})?$/.test( val ) ) + '<br>' )
}
 
</script>
 
</body>
</html>

Open in new window

0
 
rockiroadsCommented:
I tried this, note I did a test locally. Created a form with a name of xx and a textbox called fred

regxp = /^[-+]?\d{6,10}(\.\d{1,6})?$/.test(document.xx.fred.value)

so your number you used returned true  1234567890.123456

Note, using your numbers, you are specifying the whole number is between 6 and 10 digits long, are you sure about that?
0
 
techquesAuthor Commented:
No, I need dynamic but not fixed in the number, just limit it to 6 decimal places.

12.123456
131414.123456
12331.123
1231331.21444

all are ok
0
 
HonorGodCommented:
Are digits before the decimal point required?

For example, is this a valid input?

.123456

If so, the RegExp should be: /^[-+]?\d?(\.\d{1,6})?$/

If at least 1 digit is needed before the decimal point, use this, instead:

/^[-+]?\d+(\.\d{1,6})?$/






if ( /^[-+]?\d{6,10}(\.\d{1,6})?$/.test( val ) ) {
  alert( 'Valid value: "' + val + '"' )
} else {
  alert( 'Invalid value: "' + val + '"' )  
}

Open in new window

0
 
HonorGodCommented:
sigh...  one more time.
if ( /^[-+]?\d+(\.\d{1,6})?$/.test( val ) ) {
  alert( 'Valid value: "' + val + '"' )
} else {
  alert( 'Invalid value: "' + val + '"' )  
}

Open in new window

0
 
HonorGodCommented:
Thanks for the grade & points.  I'm sorry that you didn't feel it warranted an A.

Good luck & have a great day.

Happy Easter.
0
 
brandonvmooreCommented:
He didn't give you an 'A'?  Ha, makes you wish you hadn't even tried to help the ingrateful turd :)
0
 
rockiroadsCommented:
brandon Im sure he/she had their reason especially when u wonder who gave the idea of using regular expressions, lol. I just got a B from a recent question and the questioner said that was the exact solution wanted.  So you do wonder sometimes. Just one of those things
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.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now