# Numeric value convert to word format

Mahendra Darwatkar

Greenhorn

Posts: 20

posted 15 years ago

Hi !!

Can anybody guide me how can i convert value 125 to word format "One Hundred Twenty Five"

Awaiting reply...

rgds,

Mahendra

Can anybody guide me how can i convert value 125 to word format "One Hundred Twenty Five"

Awaiting reply...

rgds,

Mahendra

Frank Carver

Sheriff

Posts: 6920

Shama Khan

Ranch Hand

Posts: 185

posted 15 years ago

Without giving you each and every step. I would say that you've to do the following:

Pseudocode

----------

split the number and place into an array.

125 -

5 to be the first element of the array - hence the ones place

2 to be the second element of the array - hence the tenth place

1 to be the third element of the array - hence the hundredth place.

then you evaluate each element in the array.

if the first element is 5 then textToPrint='five'

if the second elemnet is 2 then textToPrint ='twenty'+textToPrint

if the third element is 1 then textToPrint='one hundred'+textToPrint

print(textToPrint)

------------------the end

To handle any given number, you'd have to use loops, arrays for the 'text', formatting, handle special cases (ie. 0's).

This should be enough to get you going.

Shama

Pseudocode

----------

split the number and place into an array.

125 -

5 to be the first element of the array - hence the ones place

2 to be the second element of the array - hence the tenth place

1 to be the third element of the array - hence the hundredth place.

then you evaluate each element in the array.

if the first element is 5 then textToPrint='five'

if the second elemnet is 2 then textToPrint ='twenty'+textToPrint

if the third element is 1 then textToPrint='one hundred'+textToPrint

print(textToPrint)

------------------the end

To handle any given number, you'd have to use loops, arrays for the 'text', formatting, handle special cases (ie. 0's).

This should be enough to get you going.

Shama

Shama

Zeba Mojez

Greenhorn

Posts: 10

posted 15 years ago

I think there is a better solution.

A switch statement will do a compare with every constant. So you're looking at a whole lot of compares.

What you need is a data structure that will find your information by just doing a little math. It would be really nice if Java would abstract that math away from you.

A switch statement will do a compare with every constant. So you're looking at a whole lot of compares.

What you need is a data structure that will find your information by just doing a little math. It would be really nice if Java would abstract that math away from you.

Mahendra Darwatkar

Greenhorn

Posts: 20