Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Converting Values
Message
Information générale
Forum:
Java
Catégorie:
Autre
Divers
Thread ID:
00609082
Message ID:
00609154
Vues:
23
There is probably a better and easier way but here is a routine I wrote a while back for someone who had the same problem. Sorry, it does not provide rounding but could be modified to support it.
<script language=javascript>
var ntest = 100.2
alert(setdecimal(ntest,2))
 
//setdecimal - returns value with digits past decimal position added/trimmed
//nval = numeric variable holding value to set decimal to
//lndigitspastdecimal = digits past decimal
 
function setdecimal(nval, lndigitspastdecimal) {
var lcnval = nval + ""  //ensure string type
var lnsize = lcnval.length
var decimalflag = false
 
for (curptr = 0; curptr < lnsize; curptr++) {
    chk = lcnval.charAt(curptr)
    if (chk == ".") {   //found the decimal
       var digitspastdecimal = (lnsize - curptr) - 1
       decimalflag = true
       break }
    }
if (decimalflag == false) {
   lcnval = lcnval + "."
   var digitspastdecimal = 0 }
 
var digitstoadd = lndigitspastdecimal - digitspastdecimal
if (digitstoadd > 0) {
   for (lnadd = 0; lnadd < digitstoadd; lnadd++) {
      lcnval = lcnval + "0" }
   }
if (digitstoadd < 0) {  //Crop if required - NO Rounding!
   lnsize = lnsize + digitstoadd
   lcnval = lcnval.substring(0,lnsize) }
return lcnval
}
</script>
>How can I convert or trim a value? The following represents a script running on a server that acepts a temperature value from a device in Celceus. I am converting to 'F'. I have no contol over the value exept but to read it. and it is returning a DOUBLE I only need to two decimal places... Help.
>
>
><!-- JavaScript used for temperature conversion -->
><SCRIPT LANGUAGE="JavaScript">
>	function TempDisplay(Room, RoomTemp)
>	{
>		var Temp = RoomTemp;
>		var TempF = ((Temp*1.8)+32);
>		document.write("The " + Room + " Temperature is " + TempF + "° F");
>	}
></SCRIPT>
>
><SCRIPT>
>     TempDisplay("Computer Lab", "<iLonWeb FUNCTION=ShowValue SYMBOL=NVL_RmTemp > </iLonWeb>");
></SCRIPT>
>
>
>it is returning 72.260....01
>
>Thank you in advance for your assistance...
Michael McLain
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform