Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript currency datatype to in Javascript

Status
Not open for further replies.

cricketeer1

IS-IT--Management
Dec 28, 2006
3
US
Hi all,

This is a strange problem. I support an application that implements JavaScript functions by using VBScript code (don't ask me why). So, for example, a currency to number conversion in Javascript is ultimately using Ccur VBScript function. What has started happening recently is that for some fractions a currency datatype value returned from VBScript is showing extra zeros in Javascript.

One of our developers wrote the following sample code to highlight the issue:

<html>
<body>
<script type="text/vbscript">
function test()
Dim randomNumber
randomNumber=0
randomNumber=cCur(box.value)
box2.value=randomNumber
test=randomNumber
end function
</script>

<script type="text/javascript">
function check()
{ var num;
num= test();
box1.value=num;
}
</script>

Input:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" value=" " id="box"></input>
<br>
Java script: <input type="text" value=" " id="box1"></input>
<br>
Vb script: &nbsp;&nbsp;<input type="text" value=" " id="box2"></input>
<br>
<br>
<input type="button" value="Run scripts" onClick="check()"></input>
</body>
</html>

If I run this script on my machine in IE 6 for an input like 132.2 I get Javascript: 132.20000000000002 and VBScript:132.2

I can get the proper value to display by changing the line randomNumber=cCur(box.value) to randomNumber=CDbl(cCur(box.value)).

The question is why is this happening now ? Most of the users of this application have been using IE 6 for years and never reported this problem. There is a strong feeling in my team that the problem has to do with some MS security update, but I have not found any reference to such a problem anywhere on the web.

Has anyone come across this sort of a problem?
 
What is the range of numbers and decimal places for the input in your application?

[monkey][snake] <.
 
Nevermind. Do this (highlighted area in red)
Code:
<html>
<body>
<script type="text/vbscript">
function test()
  Dim  randomNumber
  randomNumber=0
  randomNumber=cCur(box.value)
  box2.value=randomNumber
  test=randomNumber
end function
</script>

<script type="text/javascript">
function check()
{   var num;
    num= test();
    box1.value= [!]parseFloat(num).toFixed(2);[/!]
}
</script>

Input:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text"   value=" " id="box"></input>
<br>
Javascript: <input type="text"   value=" " id="box1"></input>
<br>
VBScript: &nbsp;&nbsp;<input type="text"   value=" " id="box2"></input> 
<br>
<br>
<input type="button" value="Run scripts" onClick="check()"></input>
</body>
</html>

Javascript acts funny sometimes with decimal points. Since I see you are messing with currency, then of course it will be 2 decimal places.



[monkey][snake] <.
 
Thanks monksnake!

Resolving the problem is not an issue. I was wondering if anyone had come across this issue elsewhere and found a cause for this. Or if someone runs the sample script in IE (any version less than 7) and does not get the problem do post your IE version here.
 
I was wondering if anyone had come across this issue elsewhere and found a cause for this.

This issue is well known in javascript.
The cause of it is just how javascript behaves.

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top