Solved

How javascript truncate to have maximum 6 decimal places?

Posted on 2009-04-10
14
1,848 Views
Last Modified: 2012-05-06
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
Comment
Question by:techques
[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
  • 5
  • 3
  • 3
  • +1
14 Comments
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24114394
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
 

Author Comment

by:techques
ID: 24114408
then how to change the script?
0
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24114432
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 65

Expert Comment

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

0
 

Author Comment

by:techques
ID: 24114850
I tired var regxp = /^[-+]?\d{6,10}(\.\d{1,6})?$/;

but it returns false for 1234567890.123456

why?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24114913
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 24114968
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 24115135
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
 

Author Comment

by:techques
ID: 24115408
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 24115521
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
 
LVL 41

Accepted Solution

by:
HonorGod earned 50 total points
ID: 24115527
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 24116069
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
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24118236
He didn't give you an 'A'?  Ha, makes you wish you hadn't even tried to help the ingrateful turd :)
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24118279
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…

730 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