Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBS convert string to floating point

Tags:

vbscript

Dim strnumber
strnumber = "0.3"

Dim add
add = 0.1

Dim result 
result  = strnumber +  add

MsgBox result

I want to get 0.4 as result, but get 3.1. I tried clng(strnumber) and int(strnumber), nothing works. There is a simple solution for sure but I won't find it.

EDIT: Solution

result  = CDbl(Replace(strnumber,".",",") +  add
like image 420
user2366975 Avatar asked Oct 29 '25 14:10

user2366975


2 Answers

Has to do with your locale settings. Automatic conversion (as well as explicit one) observes it in the same manner as in CStr() function.

E.g. in my locale CStr( 0.3) results to 0,3 that is invert to CDbl("0,3") while CDbl("0.3") results to an error.

BTW: always use option explicit and, for debugging purposes, On Error Goto 0

like image 71
JosefZ Avatar answered Oct 31 '25 13:10

JosefZ


Following below procedures can help:

  1. Replacing the dot(".") with comma (",") in the string
  2. change the string to double by Cdbl

example:

dim a,b,c

a="10.12"

b="5.05"

a=Replace(a,".",",")

b= Replace(b,".",",")

c=Cdbl(a)+Cdbl(b)

msgbox c
like image 25
Abhinaba Avatar answered Oct 31 '25 13:10

Abhinaba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!