posted 3 years ago

Hi JavaRanch,

I found an open-source recipe for converting numbers to words.

I modified it a bit since the requirement is only for integers until 999:

From robosoul's response at StackOverflow, I simply inserted a condition for negative integers (

True enough, the code worked and I was able to trace (pen and paper method) how it is converting the 0 and positive integers to words.

However, I am a bit lost on how it is doing the conversion for negative integers.

Can someone kindly explain

I found an open-source recipe for converting numbers to words.

I modified it a bit since the requirement is only for integers until 999:

From robosoul's response at StackOverflow, I simply inserted a condition for negative integers (

**line 12**).True enough, the code worked and I was able to trace (pen and paper method) how it is converting the 0 and positive integers to words.

However, I am a bit lost on how it is doing the conversion for negative integers.

Can someone kindly explain

*how*it is successfully converting the negative?Help first. Criticize second.

Ulf Dittmer

Rancher

Posts: 42972

73

posted 3 years ago

It's all in line 12 - the conversion for "-X" is replaced by the conversion of "--X" - which is a positive number "X", and the word "negative" is output first. Seems pretty straightforward - which aspect of it has you confused?

posted 3 years ago

Hi Ulf Dittmer,

For example, I have

I understand it will enter the first condition,

Then it will call convert(-2), which is the thing that is confusing me.

For example, I have

**ans = -2**.I understand it will enter the first condition,

**if(ans < 0)**, and it will prefix "negative " to the String.Then it will call convert(-2), which is the thing that is confusing me.

Help first. Criticize second.

Ulf Dittmer

Rancher

Posts: 42972

73

It is sorta covered in the JavaRanch Style Guide. |