views:

292

answers:

2

Is there a better way to parse percentage to double like this?

Dim Buffer As String = "50.00%"
Dim Value As Double = Double.Parse(Buffer.Replace("%",""), NumberStyles.Any, CultureInfo.InvariantCulture) / 100
+1  A: 

I'm not familiar with vb but creating a function out of it is already better

psuedo code:

function PercentToDouble( Buffer )
    return Double.Parse(Buffer.Replace("%",""), NumberStyles.Any, CultureInfo.InvariantCulture) / 100;
endfunction
BioBuckyBall
+4  A: 

The way you are doing it seems good to me.

The only point I would be careful about is that your program is assuming InvariantCulture. Make sure this is actually what you mean. For example it might be better to use the machine's default culture if your string comes from user input rather than a fixed well-defined protocol.

Mark Byers
The Invariant.Culture is just for simplicity of the sample. Stackoverflow has no user input for my sample code :-)
Dirk