Technorati label: decimal point to score, algorithm, VB
Converts the fractional sngx into a fraction, which is given in string format. Digit indicates the number of digits of the denominator of the final output score. The test results in the displayed window are as follows:
? Sngtofricstr (500.33, 1)
500 and 1/3
? Sngtofricstr (-500.4, 1)
-500 and 2/5
The core algorithm is to add black and underline that line, from http://topic.csdn.net/t/20051024/13/4346248.html 13 floor.
- Function sngtofricstr (byval sngx as single, optional digit as integer = 1) as string
- Dim I as integer
- Dim strden as string 'denominator
- Dim strnom as string 'nominator
- Dim strint as string 'integer part, record integer part
- Dim intx as integer 'rounded up the sngx
- Intx = fix (sngx)
- 'If there is an integer part, write it down; if there is no integer part, do not remember it, save the last multiple 0
- If intx <> 0 then strint = intx
- 'Decimal'
- Sngx = sngx-intx
- 'If it is negative, and there is an integer, it will take the positive, save the last like this-5.4: "-5-2/5"
- If (sngx <0) and (intx <> 0) Then sngx =-sngx
- If sngx = 0 then
- Sngtofricstr = strint
- Exit Function
- End if
- I = 1
- WhileABS (I/sngx)-round (I/sngx), 0)> 10 ^ (-digit)
- I = I + 1
- Wend
- 'Numerator
- Strnom = I
- If sngx <0 then strnom = "-" & strnom
- 'Denominator
- If sngx> = 0 then
- Strden = "/" & round (I/sngx)
- Else
- Strden = "/" & round (I/(-sngx ))
- End if
- If strden = "/1" then strden = ""
- 'Integer
- If strint <> "" Then strint = strint & "and"
- Sngtofricstr = strint & strnom & strden
- End Function