# coding problem

MOHAMMAD SALEEM

Greenhorn

Posts: 4

posted 14 years ago

Could anyone help me out in the coding of this problem:-

double d=546958.98;

The output should be:-

5 Hundred 46 Thousand 9 Hundred 58 Dollars and 98 Cents

double d=546958.98;

The output should be:-

5 Hundred 46 Thousand 9 Hundred 58 Dollars and 98 Cents

Ahmer Arman

Greenhorn

Posts: 6

posted 14 years ago

Another logic to do this.

Change your double value to String value, separeate the integer part from string, then pick from the first characters.

Make two String Arrays:

1) for numbers in words from "One" to "Ninty Nine".

2) for units "Hundreds" to so on as many u want....

check if the length is divisible by 3 or not

if yes then pick only one character from string, change it to integer and access direct to the number string array("One..."Ninty Nine"), check also for length remaining after every interation of the loop.

Pick the unit value from loop for(int loop=number.length/2 .

Ahmer Arman

Change your double value to String value, separeate the integer part from string, then pick from the first characters.

Make two String Arrays:

1) for numbers in words from "One" to "Ninty Nine".

2) for units "Hundreds" to so on as many u want....

check if the length is divisible by 3 or not

if yes then pick only one character from string, change it to integer and access direct to the number string array("One..."Ninty Nine"), check also for length remaining after every interation of the loop.

Pick the unit value from loop for(int loop=number.length/2 .

Ahmer Arman

Ahmer Arman

Cindy Glass

"The Hood"

Sheriff

Sheriff

Posts: 8521

posted 14 years ago

(copied from the other thread)

hmmmm . . . homework.

Perhaps you could convert it to a String using the toString() method of Double. Then you could identify the value of each byte by using the charAt(index) method of String.

You can find the number of chars in the whole thing by taking the String above and using the length() method. For instance your number will be 9 chars long. The last three will always be the decimal and the cents. So the "dollars" will always be at (length - 4) and (length - 5). The hundreds will always be at (length -6) the thousands and (length -8) and (length - 7) and the hundred thousands at (length - 10) and (length - 9). etc.

Is that enough of a start?

hmmmm . . . homework.

Perhaps you could convert it to a String using the toString() method of Double. Then you could identify the value of each byte by using the charAt(index) method of String.

You can find the number of chars in the whole thing by taking the String above and using the length() method. For instance your number will be 9 chars long. The last three will always be the decimal and the cents. So the "dollars" will always be at (length - 4) and (length - 5). The hundreds will always be at (length -6) the thousands and (length -8) and (length - 7) and the hundred thousands at (length - 10) and (length - 9). etc.

Is that enough of a start?

"JavaRanch, where the deer and the Certified play" - David O'Meara