TLC 6 Converting Numerals into Text
While our programs have a number of number formatting options for converting numbers into text, particularly in terms of dollars and cents, every once in a while we need something different. This video describes how to build a simple snippet to convert a number such as 3.57% into text as three point five seven percent. This video describes how to do it, and we've parked a form for actually doing it in the Section Community Treasure Chest, which is accessible after registering in the Service Center.
Transcript:
This was interesting. A situation where someone wanted to ask for a number that included decimals and convert it into words like this, three point five seven percent.
And the problem is, though the program has built into it a whole lot of formats in which to portray numbers, I'm gonna show you that now. If I add a field here for let's see. The field name here is test is the name of this three point five seven field. So if I add a field per test and I treat it as a number, I have a whole long list of possible formats to be used. These are technically called masks.
A mask is something that you apply to a number to make it look different without actually changing the number. So given the number three point five seven, I could have it look like a number with two decimal places at the end. I could have it look like some words, the words three point five seven and so forth. The problem is this word one here does not take the decimal portion of the number into account. In fact, we only have a couple of formats. Well, it's not a couple, but we only have certain formats that include the decimal portion of the number. This one here, for instance, one thousand, no decimals, is gonna truncate those decimal portions right off the end of the number, and it'll only show us the whole portion of the number.
Down here, though, if we're talking about words, we do have some formats that include both the whole portion and the decimal portion of the number, like this one, one thousand dollars and ten cents. That uses words. Right? So what if I choose that?
And then having chosen any one of these formats, you can always customize it. If I click the little custom button here, this shows me the mask that is applied to this number and then includes all kinds of weird codes. But if you look at it closely, you can actually figure out more or less what it's doing. This one is putting in the left.
The l here stands for left. It's putting in the portion of the number to the left of the decimal, then it's putting in some characters here, the word dollars with some coding in between, then it's putting in the word and, then it's putting in the right portion of the number. That's what the r means. That's the the portion to the right of the decimal place.
And then putting in some more characters, the word cents.
So we can modify this. In this case, we want the word point to appear instead of the word dollars and I'm sorry. Instead of the word and did I get that right? Let me back up.
No. No. We want the word point to appear between the two portions instead of the word dollars, and we want the word percent to appear at the end instead of the word cent. So I could go in here and change in fact, I'll go ahead and do it.
Let's replace dollars with I'm gonna replace that d with a p. The o is fine. Replacing the l with an I and the l with an n and the a with a t point.
That leaves an r that I don't want and this s that I don't want, so I'm gonna delete those. I don't want the word and, so I'm gonna delete all of that.
And it does the right portion of the number, and then instead of the word sense at the end, I want it to say percent. So I'm gonna replace the c with a p. The e is fine. Replace the n with a r. The t with a c.
I don't need that s. I replaced that with purse slash e slash n slash p. So I've customized it. I've made my own mask, which is gonna be applied to this number.
It's not gonna work yet. In fact, it's not gonna work ever, but I'll show you why. I'm showing you this because I do want you to get the idea that you can create your own custom masks here to create the to to change the appearance of these numbers. I'm gonna click okay.
I'm gonna fill this in, and let's look at what we've got so far. If I click fill, it says three point that's great. Fifty seven percent. Whoops.
No. I wanted those that fifty seven, I want that to be words as well. Let me go back into that mask.
And here where it's doing the right portion of the number, let's look at the difference here. The right portion of the number has this pound symbol, this number symbol. That means it's gonna be presented as a number. The left portion of the number, instead of the pound, it says word. That means that is gonna be presented as words. Well, we want the second portion to also be presented as words, so I'm gonna replace that pound symbol with word.
Now when I click fill, that portion is also words. It's still not right, though, because it's treating that right portion of the number, the decimal portion of the number as a single unit, fifty seven, and telling me fifty seven. What I want is to treat that right portion of number with each digit separately so that it gives me the word five followed by the word seven. Treat it as two separate numbers instead of one big number. How can we possibly do that? Well, we can't. We can't do it with just a math.
And we've added this request to our list of to our wish list of things to add to the program. So in the future, we may come up with an additional format in our list of formats that will just do this natively. But in the meantime, if we need something that's not built right into the thing, you can almost always find a way to accomplish it because the program is very flexible. It has lots of alternatives. So I'm gonna show you how we did accomplish this without using a a custom mask here.
What we did is we asked for the answer. Let's suppose the answer is three point five seven.
Then we created a derived answer. Derived answers are answers that are processed in the background. The form user never sees them. They're just little workspaces where the form author can manipulate information.
And this first field let me hit reset here. This first derived answer, let's look at it. I'll go into the field screen. It's a number, and it uses a math formula. I'm gonna click the edit button just so we can see that math formula more clear more clearly. This math formula says, give me the integer portion of the test field.
Integer is one of our what do they call it? Not operators, not formulas.
Functions. It's one of our functions here on our list of functions, integer.
There's also an absolute function, which gives you the positive value of either a positive or negative number, the round function, which rounds numbers off. Several of these functions are used to manipulate numbers. If this one, integer says, ignore the decimal portion of the number. Just give me the integer value.
So looking at the test answer, the field test, give me just the integer portion of it. Won't pull out of that. And let's look what that comes up with when our input is three point five seven. I click fill, and it comes up with three.
That makes sense. It's the integer portion of the answer. Perfect. That gives us our three.
That's how we're gonna generate this word right here.
Next, let's look at this one. I'll hit reset.
This one gets tricky.
So into the field, into our math screen. This one says, okay.
Look at the let me actually move this so we can see both it and our questions at the same time.
This one says, okay. Take the test field, that's three point five seven in this case, and subtract from it the take the test field. Subtract from it the integer portion. Oh, yeah. Yeah. Yeah. Subtract from it the integer portion of the test field.
So take three point five seven and subtract from it three.
Got that? We start with three point five seven. We subtract from it the integer portion of three point five seven. That's gonna give us point five seven.
Multiply that point five seven by ten.
That gives us five point seven.
And then take the integer of all of that, The integer portion of five point seven is five. So what we've done is we've cleverly isolated a particular digit of this number.
Start with the whole number, subtract the integer portion of it, multiply what's left by ten, which slides the decimal place over one, and then take the integer portion of that number. We have isolated the first decimal digit with this very clever formula.
Go back out of here, and when I click fill, you see it's done just what I wanted. It just grabbed out the five, the first decimal character.
Let's look at the last one. Reset.
This one, by necessity, is one bit more complicated than the last one. These are sophisticated things we're looking at today.
This one, if I go into the math formula, says, okay. So oh, and, again, let me slide this up so that we can see both things at once.
This one says, okay. Start with the whole number, three point five seven, the entire number, I should say.
Subtract the this field, integer. Subtract the integer portion. That gives us point five seven.
Also subtract the first decimal. That was five. Remember?
Five divided by ten. If I divide that five by ten, it equals point five.
Point five seven minus point five equals point o seven. I've now isolated just that third digit all by itself, o seven. Multiply that o seven by a hundred, that turns it into a seven.
A seven is exactly what we want.
Cancel out of that. Click fill. Look at that last character. See how we have we've torn apart this input number.
We put all of the integer portion of it here, and then we tear out just the first decimal place, and then we tear out just the second decimal place. Given those three portions of the number, I can put in three very straightforward fields here. The first one is just a field for the integer portion. That's this integer label right here.
Treat it as a number. Give it a word format.
Here, yield, grab the first decimal answer, that's this one here, which is gonna be, in our example, the five.
Also, treat it as a number and use words.
And the last one, just as you expect, uses the second decimal answer, treats it it as a number, and uses words.
So we've got three separate fields. This one is pulling in this number. This one's pulling in this number. This one's pulling in that number and treating them all as words.
When I click fill, I should've reset first so it would look more dramatic. So there it is. Oh oh, and I didn't show yet this condition here. I only want the word point to appear under certain conditions.
So if I go in to edit this condition, I see that this that the word point is only going to appear if it's not zero.
And then similar conditions here to control when the other portions of the portions of the number appear, but that's gonna work in all cases.
Three point five seven percent.
tags: convert, conversion, number, percentage, 3.57, point