简体   繁体   中英

Conversion of String to type double

I am having a simple drop down list as shown below which contains some values:

<select id="Opacity">
     <option value="50">50%</option>
     <option value="100">100%</option>
</select>

Now I am passing the selected value using ajax call to my webmethod:

 $("#btnGetFiles").click(function () {
                ClearImages();
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/GetFiles",
                    data: JSON.stringify({imgOpacity: $('#Opacity').val()}),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                    }
                 });
 });

Now in my webmethod I'm, getting the value as "50" if I select the drop down value as "50"

Here my problem arises that I have the below code where I need to set that opacity:

Dim imageOpacity As Single = 0.0F
imageOpacity = CInt(imgOpacity)
imageOpacity = imgOpacity / 100
imageOpacity = 1 - imgOpacity

Dim colorMatrixElements As Single()() = {New Single() {1.0F, 0.0F, 0.0F, 0.0F, 0.0F}, 
New Single() {0.0F, 1.0F, 0.0F, 0.0F, 0.0F}, 
New Single() {0.0F, 0.0F, 1.0F, 0.0F, 0.0F}, 
New Single() {0.0F, 0.0F, 0.0F, imageOpacity , 0.0F}, 
New Single() {0.0F, 0.0F, 0.0F, 0.0F, 1.0F}}

Dim wmColorMatrix As New ColorMatrix(colorMatrixElements)

Now I need to add "%" to the "imgOpacity" .If I try to do this way CInt(imgOpacity + "%") it throws me an error stating that string cannot be converted to type "Double" .

So how do I rectify this?

Why don't you just divide the value by 100? Is it not what percentage is for?


I think this is a case a typo gone un-noticed as your variable names are very similar.

See this snippet

Dim imageOpacity As Single = 1 - Convert.ToSingle(imgOpacity) / 100

Here the RHS of the third and fourth statements must take the value from imageOpacity and not from imgOpacity . Change that snippet to this.

 Dim imageOpacity As Single = 1 - Convert.ToSingle(imgOpacity) / 100 

And after that please assign imageOpacity and not imgOpacity to the matrix again

At last found the answer.

I have changed my code to this:

 imageOpacity = Single.Parse(imgOpacity)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM