I want to get ordinals from an integer - e.g. 1 -> “1st”, 21 -> “21st” and so on.
I’ve done this with this function:
[code] if i < 1 then return “”
dim ends() as string = array(“th”,“st”,“nd”,“rd”,“th”,“th”,“th”,“th”,“th”,“th”)
if i mod 100 >= 11 and i mod 100 <= 13 then
return cStr( i ) + “th”
else
return cStr( i ) + ends( i mod 10 )
end if
[/code]
which is working well for English. Is there a way of doing it for other languages - perhaps with a Shell command? (Must work on Mac and Windows) Or an MBS plugin?
I’d also quite like to have a textual option as well - so 121 -> “One hundred and twenty-first” (and other languages).
Thanks for this - that’s the sort of thing I was looking for, but I was hoping there might be a native command to do it in the locale we’re in, to save me doing it in English and French and German and Italian and… !
Thanks for this - that’s the sort of thing I was looking for, but I was hoping there might be a native command to do it in the locale we’re in, to save me doing it in English and French and German and Italian and… !
Thanks,
Hamish[/quote]
On OS X you’d need NSNumberFormatter using NSNumberFormatterSpellOutStyle
Dunno about Windows
Thanks, Norman. Not being terribly well up on Declares, I’ve put together the following, but it’s giving me a blank message box. Any chance of a shove in the right direction?
declare function NSNumberFormatter lib "Foundation" Selector "localizedStringFromNumber:numberStyle:" (input as double, type as ptr) as CFStringRef
declare function NSClassFromString Lib "Cocoa.framework" (aClassName As CFStringRef) As Ptr
dim myPtr as ptr = NSClassFromString("NSNumberFormatterNoStyle")
MsgBox( NSNumberFormatter( 5, myPtr ) )
[code]Declare Function NSClassFromString Lib “Foundation” (aClassName As CFStringRef) As Ptr
Declare Function numberWithInteger Lib “Foundation” Selector “numberWithInteger:” _
(NSNumberClass As Ptr, value As Int32) As Ptr
Declare Function localizedStringFromNumber Lib “Foundation” Selector “localizedStringFromNumber:numberStyle:” _
(NSNumberFormatter As Ptr, NSNumber As Ptr, NSNumberFormatterStyle As UInt32) As CFStringRef
Const SpellOutStyle = 5
Dim numberPtr As Ptr = numberWithInteger(NSClassFromString(“NSNumber”), 124)
Dim numberAsString = localizedStringFromNumber(NSClassFromString(“NSNumberFormatter”), numberPtr, _
SpellOutStyle)[/code]
I should note that, while Eli’s answer is dead helpful, it’s not quite answering the original question, which was to try to take e.g. 124 and convert it to 124th, but in the locale of the current user. So, if in French, it’d be 124me and so on. I can’t see anything in the NSNumberFormatter to do with this, so I’m not sure it’s possible.
The last line has a Syntax Error, should be
Dim number As String
Works also with Double
Function DoubleToSpellOutStyle(word as string) As String
dim wordvalue as Double = val(word)
Declare Function NSClassFromString Lib "Foundation" (aClassName As CFStringRef) As Ptr
Declare Function numberWithDouble Lib "Foundation" Selector "numberWithDouble:" _
(NSNumberClass As Ptr, value As Double) As Ptr
Declare Function localizedStringFromNumber Lib "Foundation" Selector _
"localizedStringFromNumber:numberStyle:" _
(NSNumberFormatter As Ptr, NSNumber As Ptr, NSNumberFormatterStyle As UInt32) As CFStringRef
Const SpellOutStyle = 5
Dim numberPtr As Ptr = numberWithDouble(NSClassFromString("NSNumber"), wordvalue)
Dim numberstring As String = localizedStringFromNumber(NSClassFromString _
("NSNumberFormatter"), numberPtr, SpellOutStyle)
return numberstring
End Function