I am pulling text from a site and would like to convert the fractions into decimal.
Group expressions using parenthesis. Brackets, or parentheses, , are used to group parts of the equation in the editor. For example: (a+b)/c will put the expression a+b on the top of the fraction but will not display the brackets. Use and ^ to insert subscripts and superscripts. Subscript and superscript can easily be done in MS Word via the font menu. In html sub- and superscript are simple tags, but the best way to write? Is ½ Hope this helps.
The fractions are in subscript though and look like the following
1¼,4½
is it possible to get around this?
Biffen4,36855 gold badges2222 silver badges3030 bronze badges
emma perkinsemma perkins30911 gold badge33 silver badges1818 bronze badges
3 Answers
Try this:
If you have any other such symbols, then simply add them to
dict
.barak manosbarak manos
24.1k77 gold badges3939 silver badges8888 bronze badges
Building on @barak manos' idea, i would just go and look for all my dictionary keys in every line and replace them on site like so:
The position of the characters you are looking for in the sentence and/or the presence of integers around them or not is irrelevant.
Ev. KounisEv. Kounis11.6k22 gold badges1818 silver badges5151 bronze badges
You must parse these numbers out from text (depends encoding), use example some parser combinator or similar. Then you can calculate decimals.
ViolaViola
Not the answer you're looking for? Browse other questions tagged python or ask your own question.
SummaryWhile some fractions are automatically available, we can also 'create' less-commonly used fractions by using Windows Character Map, Utility, Microsoft Word's Symbol Library, or Microsoft Office's Auto-Correct capability.
Details
Some fractions are available as ASCII characters (¼, ½ and ¾) and a limited number of others are available within Unicode's extended character set (⅓, ⅔, ⅛, ⅜, ⅝ and ⅞). When it becomes necessary to produce less common fractions that are not so readily available, Unicode characters give us that ability by constructing the fraction's numerator and denominator with superscripts and subscripts.
The numerators start wth ASCII code ¹ (0×185), ² (0×178) and ³ (0×179). After that, Unicode characters for ⁴ to ⁹ (H2074 to H2079) and ⁰ (H2070) should be put to use. The denominators can be generated with the Unicode characters ₀ to ₉ (H2080 to H2089). While a standard forward slash may be sufficient, I prefer the Unicode Fraction Slash (⁄ or H2044). This article will demonstrate a universal method for all Windows programs, an MS Word specific method using the Symbol library and the use of MS Office’s AutoCorrect Proofing tool to replace typed fractions with styled replacements.
A.Windows’ Character Map Utility
- Open the Character Map utility found in All Programs ► Accessories ► System Tools.
- Build your fraction by choosing each superscript or subscript digit and clicking Select.
- When the fraction is complete click Copy, then move to your target Office application and paste the completed fraction into your document.
B.Word’s Symbol Library
- MS Word’s Symbol library (Insert ► Symbol ► More Symbols) holds all of the ASCII and Unicode characters for creating your own fractions on-
the-fly.
- The image above shows the location of the denominators, the fraction slash and most numerators. The ¹, ² and ³ numerators are located much closer to the top of the symbol library.
C.MS Office’s AutoCorrect
- While the capability is there to produce stylized number display for virtually any fraction, generating more than a few of these quickly becomes tedious and a lengthy document which includes many variations will become unwieldy. Office's AutoCorrect functionality can reduce this to simply typing the digits required but even this becomes a chore after entering more than a few fraction Replace: and With: values into the Proofing tools' AutoCorrect ► Replace as you type.
I've written the following macro to quickly populate Office Proofing ► AutoCorrect ► Replace as you type entries with all of the fractions for any given denominator. Office's Proofing, AutoCorrect entries are common to all Office applications so that entry into one application (such as Excel) will have the same entry available in other Office applications with Proofing tools (such as Word). Due to subtle differences in the way Word VBA and Excel VBA handle things like the Application.InputBox command, I've chosen Excel as the preferred method of distribution. There is a sample macro-enabled workbook available through a download link toward the end of this article but you can also paste the following into Excel's Visual Basic Editor by tapping Alt+F11. Once the VBE opens, use the pull-down menus to Insert ► Module and paste this code into the new pane titled something like Book1 -Module1 (Code).
Sub mcr_Fractional_AutoCorrect_Entries_Add()
Dim i As Integer, l As Integer, n As Integer, d As Integer
Dim iNumer As Integer, iDenom As Integer, sOrig As String
Dim s As String, sNumer As String, sDenom As String
iDenom = Abs(Application.InputBox(prompt:='Please supply a “ & _
denomonator to create fractions for:', _
Title:='Denomonator for AutoCorrect Fractions', Type:=1))
If CBool(iDenom) Then
On Error Resume Next
For i = 0 To iDenom
n = i
d = iDenom
If n > 1 And n < d Then
For l = (d / 2) To 2 Step -1
If Not CBool(n Mod l) And Not CBool(d Mod l) Then
n = n / l
d = d / l
Exit For
End If
Next l
End If
sOrig = n & Chr(47) & d
sNumer = vbNullString
For l = 1 To Len(Format(n, '0'))
s = Mid(Format(n, '0'), l, 1)
Select Case s
Case 1
sNumer = sNumer & Chr(185)
Case 2, 3
sNumer = sNumer & Chr(176 + Int(s))
Case 0, 4, 5, 6, 7, 8, 9
sNumer = sNumer & ChrW(&H2070 + Int(s))
End Select
Next l
sDenom = vbNullString
For l = 1 To Len(Format(d, '0'))
s = Mid(Format(d, '0'), l, 1)
sDenom = sDenom & ChrW(&H2080 + Int(s))
Next l
Application.AutoCorrect.DeleteReplacement What:=sOrig
Application.AutoCorrect.AddReplacement sOrig, _
sNumer & ChrW(&H2044) & sDenom
Next i
End If
End Sub
- Tap Alt+Q to return to your worksheet when you have that pasted, then Alt+F8 to open the Macros dialog and Run the macro.
Supply a number for the fraction’s denominator and click OK. The corrected fractions and their original typed entries will be listed in the active worksheet starting at J2.
- Once the macro has completed, typing your fraction will result in its automatic conversion. Any AutoCorrection can be reversed to its originally typed value by tapping Ctrl+Z (Undo) immediately after the correction has been made.
If for any reason you wish to remove the fraction AutoCorrect entries, there is a remove macro supplied with the sample workbook as well.
There are a few points to make about the fractions and the macro.
·These stylized fractions are for labeling purposes. They cannot be used in calculations.
·Each fraction created is resolved down to its lowest denominator. For example: ²⁴⁄₆₀ is created as ²⁄₅.
·While these entries will be available to any Office application with Proofing tools, each Office application reads its Proofing tools into memory
the first time it is used. In other words, if Publisher has already been used at least once in the current computer session, the computer will have
to be restarted before the new AutoCorrect entries are available to Publisher.