Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number toword

Status
Not open for further replies.

idehen

Technical User
Oct 26, 2004
92
0
0
GB
Hi All,

I am using CR 2008 and i want to convert number to word.

Number is 39.4030 and i used a

formula 1: called this "Convert units to word"

ToWords(truncate({@Units to Sell},0),0)+" point "+ToWords(Val(Right(totext({@Units to Sell}),2)),0)

Then Formula 2: called this "Display"

whileprintingrecords;
stringvar array x;
redim preserve x[ubound(x)+1];
numbervar counter := 0;
stringvar result := "";

x := split({@Convert units to words}," ");
for counter := 1 to ubound(x) do(
result := result + ucase(left(x[counter],1))+mid(x[counter],2) + " ");
replace(result,"And","and");

Result:
Showing as Thirty-nine point Forty

Expected result should be:
Thirty-nine Point Forty and Thirty

I can't get this to show the thirty (30). Can someone please help.

Much appreciated.

Sol
 
Really need some help urgently with this. Is that it's impossible in crystal, cos i wouldn't have thought so.
 
Is this bit right

Number is 39.4030
and i used a formula 1: called this "Convert units to word"ToWords(truncate({@Units to Sell},0),0)+" point "+ToWords(Val(Right(totext({@Units to Sell}),2)),0)

this will return
Thirty-nine point Thirty

Ian
 
Ian,

Not sure of your question but i do think the question is right.

Sol
 
If it helps, then can you or anyone help tell how to convert to words amount such as 39.4030 in your own way.
 
Assuming decimal always .####

what about

ToWords(truncate({@Units to Sell},0),0)+" point "+ToWords(Val(left(totext(split({@Units to Sell}),".")[2],2)),0)+" and "+ToWords(Val(right(totext(split({@Units to Sell}),".")[2],2)),0)

Ian
 
I am not sure if this will work, as i do not have crystal in front of me to test (if errors, check my paren placement 1st), but maybe something like this....


ToWords(truncate({@Units to Sell},0),0)+
" point "+
ToWords(Val(LEFT(Val(RIGHT({@Units to Sell},4)),2)),0)+
ToWords(Val(Right(totext({@Units to Sell}),2)),0)



In summary:
you already have the 1st value you desire. ToWords(truncate({@Units to Sell},0),0)
you already have the last(3rd) value you desire. ToWords(Val(Right(totext({@Units to Sell}),2)),0)
you still need the middle value you desire, which hopefully this will give you. ToWords(Val(LEFT(Val(RIGHT({@Units to Sell},4)),2)),0)


 
Thanks IanWaterman.

Am getting an error message saying " A String is required here":

line highlighted below:

ToWords(Val(right(totext(split({@Units to Sell}),".")[2],2)),0)

Thanks

Sol
 
I find it very odd to interpret the decimal 4030 as "Forty and Thirty"--that seems like a slangy reference to what should be "Four Hundred and Three Thousandths".

-LB
 
Or, I guess correctly, it would be:

Four Thousand Thirty

To get "Thirty-Nine Point Four Thousand Thirty", you could use:

numbervar x := {table.number};
propercase(towords(truncate(x,0),0)+" point " +
towords(val(right(totext(x,4,""),4)),0))

-LB
 
() in wrong place

ToWords(Val(right(totext(split({@Units to Sell},".")[2]),2)),0)

Ian
 
Thanks everyone for your assistance so far.

Ian, using your formula,

39.4030 came back as Thirty-nine Point Forty and Forty.



LB, i tried your formula and am getting an error message

"A variable cannot be declared with a different type"

And as you correctly stated, am expecting in words:

Thirty-Nine Point Four Thousand Thirty

 
Just tried your formula too Fisher, and am getting the message

A string is required here (ToWords(Val(LEFT(Val(RIGHT({@Units to Sell},4)),2)),0))

Highlighted is the {Units to Sell} from the above.

Should have added this to my earlier post, but quick fingers and i clicked on submit by mistake.

Appreciate your help.
 
i am going to add my 2 cents about how it should be said.

IF i were reading this number (39.4030) to someone, to ensure it was correctly related, i would say,
"Thirtynine point four zero three zero"

after the decimal there are not twenty, thirty, forty, etc.

if i were trying to say it as a fraction, it would be "thirtynine and four hundred three thousandths"



anyway, it appears i forgot a 'totext'
(ToWords(Val(LEFT(Val(RIGHT(totext({@Units to Sell}),4)),2)),0))

 
Hi Fisher,

I am getting the same "A string is required here" using the formula suggested.

(ToWords(Val(LEFT(Val(RIGHT(totext({@Units to Sell}),4)),2)),0))
 
not having crystal in front of me, i cannot test right now, but perhaps removing the 2nd Val ?


(ToWords(Val(LEFT(RIGHT(totext({@Units to Sell}),4),2)),0))


Another thought, if there are always 2 numbers before the decimal, you could try something like:
(ToWords(Val(MID(totext({@Units to Sell}),4,2),0)))
 
That's because you are using x in your other formula, so just change the x in all places to y or z or some letter you are not referencing in your report. OR, you could change the type of variable to Local:

local numbervar x := {table.number};
propercase(towords(truncate(x,0),0)+" point " +
towords(val(right(totext(x,4,""),4)),0))

Or, you could omit the variable altogether, and just plug your number field in:

propercase(towords(truncate({@Units to Sell},0),0)+" point " +
towords(val(right(totext({@Units to Sell},4,""),4)),0))

-LB
 
Thanks very much everyone.

tried this LB:


propercase(towords(truncate({@Units to Sell},0),0)+" point " +
towords(val(right(totext({@Units to Sell},4,""),4)),0))


Worked perfectly!

Thanks alot too Fisher much appreciated everyone.
 
Wasn't sure if to post a new thread or use existing one.

The formula advised LB:
propercase(towords(truncate({@Units to Sell},0),0)+" point " +
towords(val(right(totext({@Units to Sell},4,""),4)),0))

Works for Something like 39.4030

Just wondering why it doesn't work for something like 0.03.

I changed the forumale to:
propercase(towords(truncate({@Units to Sell},0),0)+" point " +
towords(val(right(totext({@Units to Sell},2,""),2)),0))

But returns Zero point three instead of zero point zero three.

Thanks guys
 
Sorry, I didn't test it enough.

The problem is the removal of the point, and the fact that towords assumes the result should only have two decimals, as for a currency. Ignoring the decimal, then the value of 03 is 3, so it becomes necessary to add a final word that tells you it means 300 thousandths (assuming you are evaluating numbers with 4 decimals). I would use something like this then:

numbervar x := {table.number}; //number with 4 decimals
numbervar y := truncate(x,0);
numbervar z := tonumber(right(totext(x,4,""),4));
propercase(towords(y,0))+ " and " +
propercase(towords(z,0))+ " thousandths"

-LB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top