PDA

View Full Version : Putting a limit on fractional digits displayed



TimeTracker
01-16-2008, 01:26 AM
Is there a way to limit the number of digits to a decimal fraction in IE where in other browsers maxlength="10" does the job? IE usually creates a long unsightly string where perhaps only six or seven would be needed? For example, the difference between two times of birth calculated as a portion of a day does not need a long string of numbers (0.38541666666666663). How can I put a limit on IE's quotient to get something like 0.3854167?

Trinithis
01-16-2008, 02:05 AM
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Number:toFixed

TimeTracker
01-16-2008, 02:45 AM
'maxlength' already works fine in Mozilla. I added the two lines below, but it made no impact in Internet Explorer where the problem seems to be. Did I code it incorrectly?

<script type="text/javascript">
<!--

// Define Portion of Day Between Birth Times -->

function timeDifference(df){

var ETBhr = df.ETBhr.selectedIndex + 1;
var ETBmin = df.ETBmin.selectedIndex + 1;
var LBThr = df.LBThr.selectedIndex + 1;
var LBTmin = df.LBTmin.selectedIndex + 1;
var answer = df.answer.value;
var difference = [eval(df.ETBhr.selectedIndex)/24 +

eval(df.ETBmin.selectedIndex)/1440] - [eval(df.LBThr.selectedIndex)/24 +

eval(df.LBTmin.selectedIndex)/1440];
df.answer.value = difference;
var n = df.answer.value;
n.toFixed(7);
}

// -->
</script>

TimeTracker
01-16-2008, 05:56 AM
I have found two solutions to my little dilemma:
1) df.answer.value = difference.toFixed(6);
2) df.answer.value = difference.toPrecision(7);
Problem solved. Thanks!