Forum breadcrumbs - You are here:WeLoveGod RallysPublic Forums: futurebasicRe: Finding decimal equivalent of …
You need to log in to create posts and topics.
Re: Finding decimal equivalent of 45'-3 1/2 etc
77,940 Posts
#1 · February 23, 2022, 9:17 am
Quote from Forum Archives on February 23, 2022, 9:17 amPosted by: paboyle1703 <paboyle1703@...>
Ken,Do not apologize, I am grateful for your insights at any time.I like the break down into two functions, very elegant ( as always with your code)I have never used Scanner but after looking at Bernie's Scanner.incl I understand this application of it, limited knowledge is my downfall.Thanks for the code.
On Feb 23, 2022, at 02:55, Ken Shmidheiser <[email protected]> wrote:
Peter,
You're the engineer, so forgive my clumsy earlier response. I think this gives you the decimal equivalents of the fractions in feet.
One caveat with this code, is that it expects the fractions to be strictly formatted. With conversions like this, you really need a parser.
I''ll be curious to see if the calculations are correct.
Ken
include "NSLog.incl"
// Returns inch value as floating point
local fn FractionToFloat( numStr as CFStringRef ) as float
'~'1
ScannerRef scanner
CFStringRef tempStr
long wholeNumber = 0, numerator = 0, denominator = 0
float result = 0
CFRange found
if fn StringContainsString( numStr, @" " ) == NO and fn StringContainsString( numStr, @"/" ) == NO then result = fn StringFloatValue( numStr ) : exit fn
if ( fn StringContainsString( numStr, @" " ) )
found = fn StringRangeOfString( numStr, @" ")
wholeNumber = fn StringIntegerValue( fn StringSubstringToIndex( numStr, found.location ) )
tempStr = fn StringSubstringFromIndex( numStr, found.location )
scanner = fn ScannerWithString( tempStr )
if fn ScannerScanInteger( scanner, @numerator ) and fn ScannerScanCharactersFromSet( scanner, fn CharacterSetWithCharactersInString( @"/" ), NULL ) and fn ScannerScanInteger( scanner, @denominator )
result = wholeNumber + numerator/denominator
exit fn
end if
else
scanner = fn ScannerWithString( numStr )
if fn ScannerScanInteger( scanner, @numerator ) and fn ScannerScanCharactersFromSet( scanner, fn CharacterSetWithCharactersInString( @"/" ), NULL ) and fn ScannerScanInteger( scanner, @denominator )
result = numerator/denominator
exit fn
end if
end if
end fn = result
local fn ParseFraction( fractionStr as CFStringRef ) as float
'~'1
CFStringRef tempStr = NULL, footStr = NULL, inchStr = NULL
float inches = 0, result = 0
NSInteger feet = 0
CFRange found
if ( fn StringContainsString( fractionStr, @"'-" ) == NO )
result = fn FractionToFloat( fractionStr ) / 12
exit fn
else
tempStr = fn StringByReplacingOccurrencesOfString( fractionStr, @"'-0 ", @"'-" )
if ( tempStr )
found = fn StringRangeOfString( tempStr, @"'-" )
footStr = fn StringSubstringToIndex( tempStr, found.location )
feet = fn StringIntegerValue( footStr )
inchStr = fn StringSubstringFromIndex( tempStr, found.location + 2 )
inches = fn FractionToFloat( inchStr ) / 12
end if
end if
result = (float)feet + inches
end fn = result
NSLog( @"45'-3 1/2 feet = %f feet", fn ParseFraction( @"45'-3 1/2" ) )
NSLog( @"40'-3 feet = %f feet", fn ParseFraction( @"40'-3" ) )
NSLog( @"40'-0 1/2 feet = %f feet", fn ParseFraction( @"40'-0 1/2" ) )
NSLog( @"3 1/2 inches = %f feet", fn ParseFraction( @"3 1/2" ) )
NSLog( @"1/4 inch = %f feet", fn ParseFraction( @"1/4" ) )
NSLog( @"n" )
NSLog( @"%f inches", fn FractionToFloat( @"3 1/2" ) )
NSLog( @"%f inches", fn FractionToFloat( @"1/4" ) )
NSLog( @"%f inches", fn FractionToFloat( @"1/2" ) )
HandleEvents
--
To unsubscribe, send ANY message to: [email protected]
To access the list archives, go to: http://freegroups.net/groups/futurebasic/
Peter BoyleFB 7.0.11OSX 10.14.6Xcode 10.2iMac 21.5 Late 20152.8 GHz Intel Core i5
--
To unsubscribe, send ANY message to: [email protected] To access the list archives, go to: http://freegroups.net/groups/futurebasic/
Posted by: paboyle1703 <paboyle1703@...>
Ken,
Do not apologize, I am grateful for your insights at any time.
I like the break down into two functions, very elegant ( as always with your code)
I have never used Scanner but after looking at Bernie's Scanner.incl I understand this application of it, limited knowledge is my downfall.
Thanks for the code.
On Feb 23, 2022, at 02:55, Ken Shmidheiser <[email protected]> wrote:
Peter,
You're the engineer, so forgive my clumsy earlier response. I think this gives you the decimal equivalents of the fractions in feet.
One caveat with this code, is that it expects the fractions to be strictly formatted. With conversions like this, you really need a parser.
I''ll be curious to see if the calculations are correct.
Ken
include "NSLog.incl"
// Returns inch value as floating point
local fn FractionToFloat( numStr as CFStringRef ) as float
'~'1
ScannerRef scanner
CFStringRef tempStr
long wholeNumber = 0, numerator = 0, denominator = 0
float result = 0
CFRange found
if fn StringContainsString( numStr, @" " ) == NO and fn StringContainsString( numStr, @"/" ) == NO then result = fn StringFloatValue( numStr ) : exit fn
if ( fn StringContainsString( numStr, @" " ) )
found = fn StringRangeOfString( numStr, @" ")
wholeNumber = fn StringIntegerValue( fn StringSubstringToIndex( numStr, found.location ) )
tempStr = fn StringSubstringFromIndex( numStr, found.location )
scanner = fn ScannerWithString( tempStr )
if fn ScannerScanInteger( scanner, @numerator ) and fn ScannerScanCharactersFromSet( scanner, fn CharacterSetWithCharactersInString( @"/" ), NULL ) and fn ScannerScanInteger( scanner, @denominator )
result = wholeNumber + numerator/denominator
exit fn
end if
else
scanner = fn ScannerWithString( numStr )
if fn ScannerScanInteger( scanner, @numerator ) and fn ScannerScanCharactersFromSet( scanner, fn CharacterSetWithCharactersInString( @"/" ), NULL ) and fn ScannerScanInteger( scanner, @denominator )
result = numerator/denominator
exit fn
end if
end if
end fn = result
local fn ParseFraction( fractionStr as CFStringRef ) as float
'~'1
CFStringRef tempStr = NULL, footStr = NULL, inchStr = NULL
float inches = 0, result = 0
NSInteger feet = 0
CFRange found
if ( fn StringContainsString( fractionStr, @"'-" ) == NO )
result = fn FractionToFloat( fractionStr ) / 12
exit fn
else
tempStr = fn StringByReplacingOccurrencesOfString( fractionStr, @"'-0 ", @"'-" )
if ( tempStr )
found = fn StringRangeOfString( tempStr, @"'-" )
footStr = fn StringSubstringToIndex( tempStr, found.location )
feet = fn StringIntegerValue( footStr )
inchStr = fn StringSubstringFromIndex( tempStr, found.location + 2 )
inches = fn FractionToFloat( inchStr ) / 12
end if
end if
result = (float)feet + inches
end fn = result
NSLog( @"45'-3 1/2 feet = %f feet", fn ParseFraction( @"45'-3 1/2" ) )
NSLog( @"40'-3 feet = %f feet", fn ParseFraction( @"40'-3" ) )
NSLog( @"40'-0 1/2 feet = %f feet", fn ParseFraction( @"40'-0 1/2" ) )
NSLog( @"3 1/2 inches = %f feet", fn ParseFraction( @"3 1/2" ) )
NSLog( @"1/4 inch = %f feet", fn ParseFraction( @"1/4" ) )
NSLog( @"n" )
NSLog( @"%f inches", fn FractionToFloat( @"3 1/2" ) )
NSLog( @"%f inches", fn FractionToFloat( @"1/4" ) )
NSLog( @"%f inches", fn FractionToFloat( @"1/2" ) )
HandleEvents
--
To unsubscribe, send ANY message to: [email protected]
To access the list archives, go to: http://freegroups.net/groups/futurebasic/
Peter Boyle
FB 7.0.11
OSX 10.14.6
Xcode 10.2
iMac 21.5 Late 2015
2.8 GHz Intel Core i5
--
To unsubscribe, send ANY message to: [email protected] To access the list archives, go to: http://freegroups.net/groups/futurebasic/
Click for thumbs down.0Click for thumbs up.0