Decimal point to score

Source: Internet
Author: User

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.

  1. Function sngtofricstr (byval sngx as single, optional digit as integer = 1) as string
  2. Dim I as integer
  3. Dim strden as string 'denominator
  4. Dim strnom as string 'nominator
  5. Dim strint as string 'integer part, record integer part
  6. Dim intx as integer 'rounded up the sngx
  7. Intx = fix (sngx)
  8. 'If there is an integer part, write it down; if there is no integer part, do not remember it, save the last multiple 0
  9. If intx <> 0 then strint = intx
  10. 'Decimal'
  11. Sngx = sngx-intx
  12. 'If it is negative, and there is an integer, it will take the positive, save the last like this-5.4: "-5-2/5"
  13. If (sngx <0) and (intx <> 0) Then sngx =-sngx
  14. If sngx = 0 then
  15. Sngtofricstr = strint
  16. Exit Function
  17. End if
  18. I = 1
  19.  WhileABS (I/sngx)-round (I/sngx), 0)> 10 ^ (-digit)
  20. I = I + 1
  21. Wend
  22. 'Numerator
  23. Strnom = I
  24. If sngx <0 then strnom = "-" & strnom
  25. 'Denominator
  26. If sngx> = 0 then
  27. Strden = "/" & round (I/sngx)
  28. Else
  29. Strden = "/" & round (I/(-sngx ))
  30. End if
  31. If strden = "/1" then strden = ""
  32. 'Integer
  33. If strint <> "" Then strint = strint & "and"
  34. Sngtofricstr = strint & strnom & strden
  35. End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.