Very Simple Spell (Number to Words) 001 to 999 in VBAConvert number to wordsSimple Number to Words using a Single Loop String Triplets in VBAConvert number to wordsNumber to WordsConvert number into wordsDividing a number half into words and half numberChanging Number to Words in JavaScriptCode to convert number to wordsNumber-to-words translatorFunctional FrameworkConverting number to wordsPython Converting numbers to words, using Recursive
Alternatives to using writing paper for writing practice
Is `curl something | sudo bash -` a reasonably safe installation method?
What is the closed form of the following recursive function?
What exactly is the Tension force?
Commutator subgroup of Heisenberg group.
Ezek. 24:1-2, "Again in the ninth year, in the tenth month, in the tenth day of the month, ...." Which month was the tenth month?
What caused Windows ME's terrible reputation?
What impact would a dragon the size of Asia have on the environment?
What is this old "lemon-squeezer" shaped pan
Project Euler, problem # 9, Pythagorean triplet
Why do they not say "The Baby"
How can I legally visit the United States Minor Outlying Islands in the Pacific?
Concatenation using + and += operator in Python
Can I activate an iPhone without an Apple ID?
Did the Shuttle's rudder or elevons operate when flown on its carrier 747?
Redox reactions redefined
Is this a Lost Mine of Phandelver Plot Hole?
Is it rude to tell recruiters I would only change jobs for a better salary?
I quit, and boss offered me 3 month "grace period" where I could still come back
Are there any double stars that I can actually see orbit each other?
Old short story where the future emperor of the galaxy is taken for a tour around Earth
do not have power to all my breakers
Hot object in a vacuum
Meaning of slash chord without anything left of the slash
Very Simple Spell (Number to Words) 001 to 999 in VBA
Convert number to wordsSimple Number to Words using a Single Loop String Triplets in VBAConvert number to wordsNumber to WordsConvert number into wordsDividing a number half into words and half numberChanging Number to Words in JavaScriptCode to convert number to wordsNumber-to-words translatorFunctional FrameworkConverting number to wordsPython Converting numbers to words, using Recursive
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I have come across many spell number functions and have reviewed many VBA code available on the net.
The core function that gets called repeatedly is the function that converts numbers from 001 to 999 as it is the core function for conversion under the English numeral system.
I have seen that this core function is sometime unnecessarily split over sub-functions for converting one, tens, and 20s to 90s.
I have developed the following simple VBA function that takes an input as number in a string format from "001"
to "999"
and returns the output as a string.
The function uses the dash "-"
for numbers e.g. Forty-Two.
The function is easily convertible to other programming languages.
With your assistance, I am looking to further improve or simplifying the function, if possible.
You may test the function like this:
Debug.Print Do999("123")
Debug.Print Do999("001")
Debug.Print Do999("099")
Thanks in advance for your contribution.
Function Do999(ThreeDigits As String)
'-----------------------------------------
'Converts number string from 001 to 999 to Words
'Uses dash for in-between numbers from 21 to 99 for UK/US English
'Mohsen Alyafei 17 Oct 2018
'On Entry: NumIn MUST be a 3 Chars digit string "001" to "999"
'On Exit : String of number in English words
'-----------------------------------------
Dim Ones(), Tens(), dash As String, h As String, t As String, N1 As Integer, N2 As Integer
Ones = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
Tens = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", " Hundred")
'Next line is optional for English speaking words (UK, US)
If Right(ThreeDigits, 1) <> "0" Then dash = "-" 'dash as per English spelling
'------------Code starts here------------
'Get the hundreds (N1) and tens (N2)
N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1))) Else t = Ones(N2)
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)
End Function
vba numbers-to-words
$endgroup$
add a comment |
$begingroup$
I have come across many spell number functions and have reviewed many VBA code available on the net.
The core function that gets called repeatedly is the function that converts numbers from 001 to 999 as it is the core function for conversion under the English numeral system.
I have seen that this core function is sometime unnecessarily split over sub-functions for converting one, tens, and 20s to 90s.
I have developed the following simple VBA function that takes an input as number in a string format from "001"
to "999"
and returns the output as a string.
The function uses the dash "-"
for numbers e.g. Forty-Two.
The function is easily convertible to other programming languages.
With your assistance, I am looking to further improve or simplifying the function, if possible.
You may test the function like this:
Debug.Print Do999("123")
Debug.Print Do999("001")
Debug.Print Do999("099")
Thanks in advance for your contribution.
Function Do999(ThreeDigits As String)
'-----------------------------------------
'Converts number string from 001 to 999 to Words
'Uses dash for in-between numbers from 21 to 99 for UK/US English
'Mohsen Alyafei 17 Oct 2018
'On Entry: NumIn MUST be a 3 Chars digit string "001" to "999"
'On Exit : String of number in English words
'-----------------------------------------
Dim Ones(), Tens(), dash As String, h As String, t As String, N1 As Integer, N2 As Integer
Ones = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
Tens = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", " Hundred")
'Next line is optional for English speaking words (UK, US)
If Right(ThreeDigits, 1) <> "0" Then dash = "-" 'dash as per English spelling
'------------Code starts here------------
'Get the hundreds (N1) and tens (N2)
N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1))) Else t = Ones(N2)
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)
End Function
vba numbers-to-words
$endgroup$
$begingroup$
I meant programming languages. Sure languages using the Masculine and Feminine such as French and Arabic will need lots of modifications.
$endgroup$
– Mohsen Alyafei
Jul 6 at 11:45
$begingroup$
The rules for the conversion are ad-hoc, so fundamental simplification isn't likely. One obvious area of improvement is to extend the functionality so that it applies to more than three digits. A minor improvement would be to make the inputVariant
so that you could pass it either an integer variable containing a 3-digit number or a string variable and have it work in either case without a type mismatch error.
$endgroup$
– John Coleman
Jul 6 at 12:21
$begingroup$
@JohnColeman Thanks John. This function is a core sub-function that will be called many times from a main function to convert vary large numbers. The main function will take care of the dirty work and will always pass a 3 digit number string for conversion. Integer conversion happens at the higher level function. The higher function will deal with any length of number from 0 to a Decillion.
$endgroup$
– Mohsen Alyafei
Jul 6 at 12:42
add a comment |
$begingroup$
I have come across many spell number functions and have reviewed many VBA code available on the net.
The core function that gets called repeatedly is the function that converts numbers from 001 to 999 as it is the core function for conversion under the English numeral system.
I have seen that this core function is sometime unnecessarily split over sub-functions for converting one, tens, and 20s to 90s.
I have developed the following simple VBA function that takes an input as number in a string format from "001"
to "999"
and returns the output as a string.
The function uses the dash "-"
for numbers e.g. Forty-Two.
The function is easily convertible to other programming languages.
With your assistance, I am looking to further improve or simplifying the function, if possible.
You may test the function like this:
Debug.Print Do999("123")
Debug.Print Do999("001")
Debug.Print Do999("099")
Thanks in advance for your contribution.
Function Do999(ThreeDigits As String)
'-----------------------------------------
'Converts number string from 001 to 999 to Words
'Uses dash for in-between numbers from 21 to 99 for UK/US English
'Mohsen Alyafei 17 Oct 2018
'On Entry: NumIn MUST be a 3 Chars digit string "001" to "999"
'On Exit : String of number in English words
'-----------------------------------------
Dim Ones(), Tens(), dash As String, h As String, t As String, N1 As Integer, N2 As Integer
Ones = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
Tens = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", " Hundred")
'Next line is optional for English speaking words (UK, US)
If Right(ThreeDigits, 1) <> "0" Then dash = "-" 'dash as per English spelling
'------------Code starts here------------
'Get the hundreds (N1) and tens (N2)
N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1))) Else t = Ones(N2)
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)
End Function
vba numbers-to-words
$endgroup$
I have come across many spell number functions and have reviewed many VBA code available on the net.
The core function that gets called repeatedly is the function that converts numbers from 001 to 999 as it is the core function for conversion under the English numeral system.
I have seen that this core function is sometime unnecessarily split over sub-functions for converting one, tens, and 20s to 90s.
I have developed the following simple VBA function that takes an input as number in a string format from "001"
to "999"
and returns the output as a string.
The function uses the dash "-"
for numbers e.g. Forty-Two.
The function is easily convertible to other programming languages.
With your assistance, I am looking to further improve or simplifying the function, if possible.
You may test the function like this:
Debug.Print Do999("123")
Debug.Print Do999("001")
Debug.Print Do999("099")
Thanks in advance for your contribution.
Function Do999(ThreeDigits As String)
'-----------------------------------------
'Converts number string from 001 to 999 to Words
'Uses dash for in-between numbers from 21 to 99 for UK/US English
'Mohsen Alyafei 17 Oct 2018
'On Entry: NumIn MUST be a 3 Chars digit string "001" to "999"
'On Exit : String of number in English words
'-----------------------------------------
Dim Ones(), Tens(), dash As String, h As String, t As String, N1 As Integer, N2 As Integer
Ones = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
Tens = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", " Hundred")
'Next line is optional for English speaking words (UK, US)
If Right(ThreeDigits, 1) <> "0" Then dash = "-" 'dash as per English spelling
'------------Code starts here------------
'Get the hundreds (N1) and tens (N2)
N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1))) Else t = Ones(N2)
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)
End Function
vba numbers-to-words
vba numbers-to-words
edited Jul 6 at 16:31
200_success
134k21 gold badges171 silver badges441 bronze badges
134k21 gold badges171 silver badges441 bronze badges
asked Jul 6 at 11:28
Mohsen AlyafeiMohsen Alyafei
516 bronze badges
516 bronze badges
$begingroup$
I meant programming languages. Sure languages using the Masculine and Feminine such as French and Arabic will need lots of modifications.
$endgroup$
– Mohsen Alyafei
Jul 6 at 11:45
$begingroup$
The rules for the conversion are ad-hoc, so fundamental simplification isn't likely. One obvious area of improvement is to extend the functionality so that it applies to more than three digits. A minor improvement would be to make the inputVariant
so that you could pass it either an integer variable containing a 3-digit number or a string variable and have it work in either case without a type mismatch error.
$endgroup$
– John Coleman
Jul 6 at 12:21
$begingroup$
@JohnColeman Thanks John. This function is a core sub-function that will be called many times from a main function to convert vary large numbers. The main function will take care of the dirty work and will always pass a 3 digit number string for conversion. Integer conversion happens at the higher level function. The higher function will deal with any length of number from 0 to a Decillion.
$endgroup$
– Mohsen Alyafei
Jul 6 at 12:42
add a comment |
$begingroup$
I meant programming languages. Sure languages using the Masculine and Feminine such as French and Arabic will need lots of modifications.
$endgroup$
– Mohsen Alyafei
Jul 6 at 11:45
$begingroup$
The rules for the conversion are ad-hoc, so fundamental simplification isn't likely. One obvious area of improvement is to extend the functionality so that it applies to more than three digits. A minor improvement would be to make the inputVariant
so that you could pass it either an integer variable containing a 3-digit number or a string variable and have it work in either case without a type mismatch error.
$endgroup$
– John Coleman
Jul 6 at 12:21
$begingroup$
@JohnColeman Thanks John. This function is a core sub-function that will be called many times from a main function to convert vary large numbers. The main function will take care of the dirty work and will always pass a 3 digit number string for conversion. Integer conversion happens at the higher level function. The higher function will deal with any length of number from 0 to a Decillion.
$endgroup$
– Mohsen Alyafei
Jul 6 at 12:42
$begingroup$
I meant programming languages. Sure languages using the Masculine and Feminine such as French and Arabic will need lots of modifications.
$endgroup$
– Mohsen Alyafei
Jul 6 at 11:45
$begingroup$
I meant programming languages. Sure languages using the Masculine and Feminine such as French and Arabic will need lots of modifications.
$endgroup$
– Mohsen Alyafei
Jul 6 at 11:45
$begingroup$
The rules for the conversion are ad-hoc, so fundamental simplification isn't likely. One obvious area of improvement is to extend the functionality so that it applies to more than three digits. A minor improvement would be to make the input
Variant
so that you could pass it either an integer variable containing a 3-digit number or a string variable and have it work in either case without a type mismatch error.$endgroup$
– John Coleman
Jul 6 at 12:21
$begingroup$
The rules for the conversion are ad-hoc, so fundamental simplification isn't likely. One obvious area of improvement is to extend the functionality so that it applies to more than three digits. A minor improvement would be to make the input
Variant
so that you could pass it either an integer variable containing a 3-digit number or a string variable and have it work in either case without a type mismatch error.$endgroup$
– John Coleman
Jul 6 at 12:21
$begingroup$
@JohnColeman Thanks John. This function is a core sub-function that will be called many times from a main function to convert vary large numbers. The main function will take care of the dirty work and will always pass a 3 digit number string for conversion. Integer conversion happens at the higher level function. The higher function will deal with any length of number from 0 to a Decillion.
$endgroup$
– Mohsen Alyafei
Jul 6 at 12:42
$begingroup$
@JohnColeman Thanks John. This function is a core sub-function that will be called many times from a main function to convert vary large numbers. The main function will take care of the dirty work and will always pass a 3 digit number string for conversion. Integer conversion happens at the higher level function. The higher function will deal with any length of number from 0 to a Decillion.
$endgroup$
– Mohsen Alyafei
Jul 6 at 12:42
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Code Formatting
What certainly could be improved is the indentation of your code for better readability:
Function Do999(ThreeDigits As String)
'Everything after your function header until End Function should be indented
' ...
End Function
Same for further conditional code blocks:
N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then
t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1)))
Else
t = Ones(N2)
End If
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)
Function naming
Do999
isn't very clear / self descriptive about what the function does.Convert0UpTo999ToWords
might be a better choice for example.
Number output in words
The function uses the dash
"-"
for numbers e.g. Forty-Two.
That's not how numbers are naturally written in words. Usually 42
would be written as forty-two
.
Avoid the 1st letter of the number words to be capitalized.
$endgroup$
$begingroup$
Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:29
$begingroup$
@MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
$endgroup$
– πάντα ῥεῖ
Jul 6 at 13:33
$begingroup$
Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:37
add a comment |
$begingroup$
The word "hundred" doesn't belong in the Tens
array:
- It is the only element in the array that starts with a space
- It is only accessed at one place in the code
- It is never accessed from the place in the code where the other elements are accessed
Therefore it is better to either:
- Declare it as a single string variable
- Just use the string
" hundred"
literally, directly in the one place where it is needed
Another strange thing is that the dash
variable contains a dash in most cases but sometimes also contains nothing at all. In the latter case its variable name dash
doesn't accurately describe what the variable contains. Rename it to separator
or sep
instead.
$endgroup$
$begingroup$
Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
$endgroup$
– Mohsen Alyafei
Jul 6 at 16:10
$begingroup$
It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
$endgroup$
– Roland Illig
Jul 6 at 18:40
$begingroup$
True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:05
$begingroup$
The empty string is great. It just shouldn't be stored in a variable whose name isdash
orhyphen
, since then the name of the variable does not match what the variable contains.
$endgroup$
– Roland Illig
Jul 6 at 19:41
$begingroup$
I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:51
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f223615%2fvery-simple-spell-number-to-words-001-to-999-in-vba%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Code Formatting
What certainly could be improved is the indentation of your code for better readability:
Function Do999(ThreeDigits As String)
'Everything after your function header until End Function should be indented
' ...
End Function
Same for further conditional code blocks:
N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then
t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1)))
Else
t = Ones(N2)
End If
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)
Function naming
Do999
isn't very clear / self descriptive about what the function does.Convert0UpTo999ToWords
might be a better choice for example.
Number output in words
The function uses the dash
"-"
for numbers e.g. Forty-Two.
That's not how numbers are naturally written in words. Usually 42
would be written as forty-two
.
Avoid the 1st letter of the number words to be capitalized.
$endgroup$
$begingroup$
Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:29
$begingroup$
@MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
$endgroup$
– πάντα ῥεῖ
Jul 6 at 13:33
$begingroup$
Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:37
add a comment |
$begingroup$
Code Formatting
What certainly could be improved is the indentation of your code for better readability:
Function Do999(ThreeDigits As String)
'Everything after your function header until End Function should be indented
' ...
End Function
Same for further conditional code blocks:
N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then
t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1)))
Else
t = Ones(N2)
End If
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)
Function naming
Do999
isn't very clear / self descriptive about what the function does.Convert0UpTo999ToWords
might be a better choice for example.
Number output in words
The function uses the dash
"-"
for numbers e.g. Forty-Two.
That's not how numbers are naturally written in words. Usually 42
would be written as forty-two
.
Avoid the 1st letter of the number words to be capitalized.
$endgroup$
$begingroup$
Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:29
$begingroup$
@MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
$endgroup$
– πάντα ῥεῖ
Jul 6 at 13:33
$begingroup$
Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:37
add a comment |
$begingroup$
Code Formatting
What certainly could be improved is the indentation of your code for better readability:
Function Do999(ThreeDigits As String)
'Everything after your function header until End Function should be indented
' ...
End Function
Same for further conditional code blocks:
N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then
t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1)))
Else
t = Ones(N2)
End If
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)
Function naming
Do999
isn't very clear / self descriptive about what the function does.Convert0UpTo999ToWords
might be a better choice for example.
Number output in words
The function uses the dash
"-"
for numbers e.g. Forty-Two.
That's not how numbers are naturally written in words. Usually 42
would be written as forty-two
.
Avoid the 1st letter of the number words to be capitalized.
$endgroup$
Code Formatting
What certainly could be improved is the indentation of your code for better readability:
Function Do999(ThreeDigits As String)
'Everything after your function header until End Function should be indented
' ...
End Function
Same for further conditional code blocks:
N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then
t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1)))
Else
t = Ones(N2)
End If
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)
Function naming
Do999
isn't very clear / self descriptive about what the function does.Convert0UpTo999ToWords
might be a better choice for example.
Number output in words
The function uses the dash
"-"
for numbers e.g. Forty-Two.
That's not how numbers are naturally written in words. Usually 42
would be written as forty-two
.
Avoid the 1st letter of the number words to be capitalized.
edited Jul 7 at 9:44
Vogel612♦
22.1k5 gold badges48 silver badges131 bronze badges
22.1k5 gold badges48 silver badges131 bronze badges
answered Jul 6 at 12:59
πάντα ῥεῖπάντα ῥεῖ
4,0293 gold badges14 silver badges28 bronze badges
4,0293 gold badges14 silver badges28 bronze badges
$begingroup$
Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:29
$begingroup$
@MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
$endgroup$
– πάντα ῥεῖ
Jul 6 at 13:33
$begingroup$
Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:37
add a comment |
$begingroup$
Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:29
$begingroup$
@MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
$endgroup$
– πάντα ῥεῖ
Jul 6 at 13:33
$begingroup$
Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:37
$begingroup$
Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:29
$begingroup$
Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:29
$begingroup$
@MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
$endgroup$
– πάντα ῥεῖ
Jul 6 at 13:33
$begingroup$
@MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
$endgroup$
– πάντα ῥεῖ
Jul 6 at 13:33
$begingroup$
Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:37
$begingroup$
Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:37
add a comment |
$begingroup$
The word "hundred" doesn't belong in the Tens
array:
- It is the only element in the array that starts with a space
- It is only accessed at one place in the code
- It is never accessed from the place in the code where the other elements are accessed
Therefore it is better to either:
- Declare it as a single string variable
- Just use the string
" hundred"
literally, directly in the one place where it is needed
Another strange thing is that the dash
variable contains a dash in most cases but sometimes also contains nothing at all. In the latter case its variable name dash
doesn't accurately describe what the variable contains. Rename it to separator
or sep
instead.
$endgroup$
$begingroup$
Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
$endgroup$
– Mohsen Alyafei
Jul 6 at 16:10
$begingroup$
It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
$endgroup$
– Roland Illig
Jul 6 at 18:40
$begingroup$
True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:05
$begingroup$
The empty string is great. It just shouldn't be stored in a variable whose name isdash
orhyphen
, since then the name of the variable does not match what the variable contains.
$endgroup$
– Roland Illig
Jul 6 at 19:41
$begingroup$
I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:51
|
show 1 more comment
$begingroup$
The word "hundred" doesn't belong in the Tens
array:
- It is the only element in the array that starts with a space
- It is only accessed at one place in the code
- It is never accessed from the place in the code where the other elements are accessed
Therefore it is better to either:
- Declare it as a single string variable
- Just use the string
" hundred"
literally, directly in the one place where it is needed
Another strange thing is that the dash
variable contains a dash in most cases but sometimes also contains nothing at all. In the latter case its variable name dash
doesn't accurately describe what the variable contains. Rename it to separator
or sep
instead.
$endgroup$
$begingroup$
Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
$endgroup$
– Mohsen Alyafei
Jul 6 at 16:10
$begingroup$
It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
$endgroup$
– Roland Illig
Jul 6 at 18:40
$begingroup$
True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:05
$begingroup$
The empty string is great. It just shouldn't be stored in a variable whose name isdash
orhyphen
, since then the name of the variable does not match what the variable contains.
$endgroup$
– Roland Illig
Jul 6 at 19:41
$begingroup$
I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:51
|
show 1 more comment
$begingroup$
The word "hundred" doesn't belong in the Tens
array:
- It is the only element in the array that starts with a space
- It is only accessed at one place in the code
- It is never accessed from the place in the code where the other elements are accessed
Therefore it is better to either:
- Declare it as a single string variable
- Just use the string
" hundred"
literally, directly in the one place where it is needed
Another strange thing is that the dash
variable contains a dash in most cases but sometimes also contains nothing at all. In the latter case its variable name dash
doesn't accurately describe what the variable contains. Rename it to separator
or sep
instead.
$endgroup$
The word "hundred" doesn't belong in the Tens
array:
- It is the only element in the array that starts with a space
- It is only accessed at one place in the code
- It is never accessed from the place in the code where the other elements are accessed
Therefore it is better to either:
- Declare it as a single string variable
- Just use the string
" hundred"
literally, directly in the one place where it is needed
Another strange thing is that the dash
variable contains a dash in most cases but sometimes also contains nothing at all. In the latter case its variable name dash
doesn't accurately describe what the variable contains. Rename it to separator
or sep
instead.
answered Jul 6 at 15:18
Roland IlligRoland Illig
14.1k1 gold badge23 silver badges56 bronze badges
14.1k1 gold badge23 silver badges56 bronze badges
$begingroup$
Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
$endgroup$
– Mohsen Alyafei
Jul 6 at 16:10
$begingroup$
It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
$endgroup$
– Roland Illig
Jul 6 at 18:40
$begingroup$
True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:05
$begingroup$
The empty string is great. It just shouldn't be stored in a variable whose name isdash
orhyphen
, since then the name of the variable does not match what the variable contains.
$endgroup$
– Roland Illig
Jul 6 at 19:41
$begingroup$
I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:51
|
show 1 more comment
$begingroup$
Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
$endgroup$
– Mohsen Alyafei
Jul 6 at 16:10
$begingroup$
It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
$endgroup$
– Roland Illig
Jul 6 at 18:40
$begingroup$
True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:05
$begingroup$
The empty string is great. It just shouldn't be stored in a variable whose name isdash
orhyphen
, since then the name of the variable does not match what the variable contains.
$endgroup$
– Roland Illig
Jul 6 at 19:41
$begingroup$
I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:51
$begingroup$
Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
$endgroup$
– Mohsen Alyafei
Jul 6 at 16:10
$begingroup$
Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
$endgroup$
– Mohsen Alyafei
Jul 6 at 16:10
$begingroup$
It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
$endgroup$
– Roland Illig
Jul 6 at 18:40
$begingroup$
It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
$endgroup$
– Roland Illig
Jul 6 at 18:40
$begingroup$
True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:05
$begingroup$
True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:05
$begingroup$
The empty string is great. It just shouldn't be stored in a variable whose name is
dash
or hyphen
, since then the name of the variable does not match what the variable contains.$endgroup$
– Roland Illig
Jul 6 at 19:41
$begingroup$
The empty string is great. It just shouldn't be stored in a variable whose name is
dash
or hyphen
, since then the name of the variable does not match what the variable contains.$endgroup$
– Roland Illig
Jul 6 at 19:41
$begingroup$
I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:51
$begingroup$
I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:51
|
show 1 more comment
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f223615%2fvery-simple-spell-number-to-words-001-to-999-in-vba%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
I meant programming languages. Sure languages using the Masculine and Feminine such as French and Arabic will need lots of modifications.
$endgroup$
– Mohsen Alyafei
Jul 6 at 11:45
$begingroup$
The rules for the conversion are ad-hoc, so fundamental simplification isn't likely. One obvious area of improvement is to extend the functionality so that it applies to more than three digits. A minor improvement would be to make the input
Variant
so that you could pass it either an integer variable containing a 3-digit number or a string variable and have it work in either case without a type mismatch error.$endgroup$
– John Coleman
Jul 6 at 12:21
$begingroup$
@JohnColeman Thanks John. This function is a core sub-function that will be called many times from a main function to convert vary large numbers. The main function will take care of the dirty work and will always pass a 3 digit number string for conversion. Integer conversion happens at the higher level function. The higher function will deal with any length of number from 0 to a Decillion.
$endgroup$
– Mohsen Alyafei
Jul 6 at 12:42