Forum Moderators: open

Message Too Old, No Replies

Convert string to a double?

         

benj0323

10:38 pm on Feb 27, 2005 (gmt 0)

10+ Year Member



I need to convert a string to a double. Basically this is what I'm doing here.

function sum_statements(value)
{
total_statements += value;
}

then....

sum_statements(5.00);

Any ideas?

Thanks for your help.

Rambo Tribble

12:11 am on Feb 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Strings convert automatically when you use them in a mathematical operation.

<script type="text/javascript">
var a=".5";
var b="2.9999";
alert(b/a);
</script>

rocknbil

4:46 pm on Feb 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(Rambo is parseFloat() execessive overhead for the same purpose?)

benj if you're asking what I **think** you're asking - how to get a two-digit (monetary) decimal part from every calculation regardless of whether it's fractional or not - the only consistent solution I've found (which may not add up to much! :-) )is to pick apart the number as a string and reassemble it. This may not be the best way, but it works:

<form>
<input type="text" size="12" name="decimal" value="">
<input type="button"onClick="calcDecimal(this.form);" value="Calculate Monetary">
</form>

<script language="Javascript">

// This can be any function that does your math. Each time
// you want a monetary format, pass the number to
// truncate, it will return xx.xx

function calcDecimal(form) {
if (isNaN(form.decimal.value)) { alert('Come on, it has to be a number!'); return; }
var monetary = truncate(form.decimal.value);
alert(monetary);

}

function truncate(num) {
var str = num + ''; // Now it's a string.
if (str.indexOf('.') == -1) { return str + '.00'; }
dot = str.length - str.indexOf('.');
if (dot > 3) { return str.substring(0,str.length-dot+3); }
else if (dot == 2) { return str + '0'; }
return str;
}
</script>

SpaceFrog

4:51 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



just to bring another version :
<script type='text/javascript'>
function ExactRound(a,b,e){
a=String(a)
b=String(b)
var deci=( a.split('\.')[1].length > b.split('\.')[1].length )?a.split('\.')[1].length:b.split('\.')[1].length;
var c= Number(a) + Number(b);
var expo= (Math.pow(10,deci))
var result=((Math.round(c*expo)/expo).toFixed(e));
return result;
}
</script>

</head>

<body>
<script type='text/javascript'>
alert(ExactRound(70.8021,86.104,4))
</script>


</body>

</html>

Rambo Tribble

11:36 pm on Feb 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, I'm very much a fan of parseInt() and parseFloat(). I'm less enamored of parsnips.

rocknbil

12:41 am on Mar 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



^ ^ LOL . . . . Let's not forget corned beef associative hash. There's more than one way to cook it.

I was serious. :-) I often miss some Really Important Stuff in the interest of getting the project done.

Rambo Tribble

2:57 am on Mar 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, if we must be serious, I like the parseInt() and parseFloat() methods because they remove ambiguity from code. I doubt any significant performance difference exists within the interpreter between automatic and forced parsing. I can only imagine the network bandwidth considerations would characteristically be negligible, though, obviously, we've added 10 to 12 bytes to the file with each invocation.

Am I to take it, rocknbill, that the toFixed() method of the Number object doesn't work for you? Are you coding for older browsers?

benj0323

7:33 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Thanks a lot for your help. I got the problem fixed thanks to this thread.

I kept getting NaN as my variable value.

Rambo Tribble

2:34 pm on Mar 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It might be worth adding, toFixed() returns a string, not, as one might expect, a number.

I should also note that there are those who feel multiplying or dividing a string by 1 is a more efficient way to convert the string to a number, as compared with using the Parse methods. That argument is not without some shred of merit, so I find the best policy is to simply ignore it (ignorance is the best tool there is for for avoiding confusion).

rocknbil

4:20 pm on Mar 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No I don't use toFixed() often, or at all. I asked just in trying to fine tune my methods and always wonder if something is better accomplished in another way. I often get difficulties in Javascript with number concatenating instead of adding, as in 23 + 45 = 2345. UGH! I've tried all the usual suspects, setting the data type, parsing as integer/float, etc . . . . the only solution I've found that works reliably is as you said, a convoluted multiply-then-divide math to get the numbers to act like numbers.

Didn't mean to jack the thread but that's actually the reason I originally asked.