我使用的方法需要单值来指定百分比,例如0.8F
的值将表示80%
,值为{{ 1}}将意味着0.921F
,但为了使代码更具可读性,我更愿意发送 Double 值,例如,如果我发送值92,1%
它应该是80.0R
,应将其转换为80%
,0.8F
的值应为80.5R
,并且应转换为80,5%
(如果我的计算结果良好) )0.805F
的值应转换为100.0R
。
我的问题是,当我尝试将Double Value(例如1.0F
)转换为Single时,我会得到一个糟糕的翻译,在这种情况下我得到80.0R
,但应该是80.0F
我做错了什么?
这是方法:
0.8F
用法:
Private Function FindImage(ByVal BaseImage As Bitmap,
ByVal ImageToFind As Bitmap,
ByVal Similarity As Double) As AForge.Imaging.TemplateMatch()
Dim SingleSimilarity As Single
' Translate the readable similarity percent value to Single value.
Select Case Similarity
Case Is < 0.1R, Is > 100.0R ' Value is out of range.
Throw New Exception(String.Format("Similarity value of '{0}' is out of range, range is from '0.1' to '100.0'",
CStr(Similarity)))
Case Is = 100.0R ' Identical image comparission.
SingleSimilarity = 1.0F
Case Else ' Image with similarity comparission.
SingleSimilarity = Convert.ToSingle(...) ' Here goes bad.
End Select
...
' This only accepts a Single value:
Dim tm As New AForge.Imaging.ExhaustiveTemplateMatching(SingleSimilarity)
...
End Function
答案 0 :(得分:2)
Convert.ToSingle()
只是将您的号码从Double
转换为Single
。它不会对值进行任何修改。因此80.5R
变为80.5F
- 价值没有变化。
您需要实际编写转换以从百分比转换 - 如下所示:
Case Else
SingleSimilarity = Convert.ToSingle(Similarity) / 100.0F
答案 1 :(得分:1)
这是我见过的最简单的代码。
SingleSimilarity = single.parse(Similarity) / 100.0F