Hi, to everyone.‎

My program to make program to write the application the input ‎a dollar amount to be printed on a cheque, and then prints the ‎amount with eleven position number are available for printing ‎the cheque. And need to run the word equivalent of the cheque ‎amount e.g.: the amount 123.30 should be written
One hundred twenty three dollars and thirty cents ‎

My problem I don't know how to make to run the number to ‎the word equivalent of the cheque amount. Like this
The amount 123.30 should be written as One hundred twenty three dollars and thirty cents ‎

‎ This is my code:‎

import javax.swing.*;‎
‎/**‎
‎ * <p>Title: </p>‎
‎ * <p>Description: </p>‎
‎ * <p>Copyright: Copyright (c) 2005</p>‎
‎ * <p>Company: </p>‎
‎ * @author not attributable‎
‎ * @version 1.0‎
‎ */‎
public class Cheque
‎       {‎
‎        public static void main (String [] args)‎
‎        {‎
‎          String Q = JOptionPane.showInputDialog(null, "Please ‎Enter the Account..");‎

‎          int l;‎
‎          int nn=0;‎
‎          int t=0;‎
‎         for (int x=1 ; x<=Q.length() ; x++)‎
‎         {‎
‎           if (Q.length() < 12) {‎
‎             l = Q.length();‎
‎             t = l - 12;‎
‎             nn = t * ( -1);‎
‎           }‎
‎       }‎
‎         System.out.println(  " "+Q +"$"+" t The Cheque amount ‎‎" );‎
‎        System.out.println( " " + nn+ " t The positon number");‎
‎            }‎
‎        }‎

can anyone tell me how can i do that.Thank you,

Dani AI

Generated

Brief, practical plan that ties the existing replies together and fills gaps in implementation and edge cases.

Start by treating the input as money, not as a floating point number — use a decimal type that keeps exact cents, then convert the whole amount to an integer number of cents. Break that integer into dollars and cents (dollars = cents/100, cents = cents%100). Convert the dollar portion by grouping digits in threes (units, thousands, millions, ...). For each three-digit group produce a phrase like "X hundred Y" (with a small routine to handle 0–99 including the 10–19 special cases) and then append the scale name ("thousand", "million", ...). Use constant arrays or enums for unit/teen/tens/scale words as recommended, and refer to for the idea of special handling of teens and tens.

Java-specific notes and a compact pattern for parsing/printing:

  • Parse with BigDecimal and normalize to two decimals to avoid rounding surprises.
  • Convert to total cents with movePointRight(2) and longValueExact().
  • Use a small helper to convert a 0–999 integer to words; iterate groups and join non-empty group words with their scale.
  • Keep pluralization simple: "dollar" when 1, else "dollars"; similarly for "cent(s)".

Example (conceptual) Java snippet for safe parsing and fixed-width printing:

BigDecimal amt = new BigDecimal(input.trim()).setScale(2, RoundingMode.HALF_EVEN);
long totalCents = amt.movePointRight(2).longValueExact();
long dollars = totalCents / 100;
int cents = (int)(totalCents % 100);
String rightJust11 = String.format("%11s", amt.toPlainString());

Troubleshooting and style: validate input (empty, non-numeric, negative), decide on cheque wording style ("and 30/100 dollars" vs "and thirty cents"), handle 0 dollars or 0 cents explicitly, and add unit tests for edge cases (.05, 0.00, 1000000000.00). This gives a robust, legal-friendly output and a clean 11-position numeric field for printing on cheques.

Recommended Answers

All 2 Replies

Simple global constants should do.

Take $123.30.

1. First you need to separate the dollars and the cents using the substring method of the String class (see code below).

2. How do you convert a hundreds digit? Just put "Hundred" afterwards:

1xx = > One Hundred xx

3. How do you convert a tens digit? This one is harder.

If the tens digit is 1, then it is easiest to just handle every possible case (11-19) in a big if statement.

11=>"Eleven"
..
19=>"Nineteen"

If the tens digit is a 2 then its always "Twenty" followed by the rest.

21 => "Twenty One"

...

If the tens digit is a 9 then its always "Ninety" followed by the rest.

97 => "Ninety Seven"

4. How do you obtain the digits? After separating the dollars from the cents, use the charAt method of the String class.

Run the code below for a simple example of obtaining the digits:

class NumToWord 
{
	public static void main(String[] a)
	{
		
		//define string
		String text = "123.30";
	
		//find position of decimal in text
		int decimalPos = text.indexOf(".");
		
		//get text before decimal
		String dollars = text.substring(0, decimalPos);
	
		//get text after decimal
		String cents = text.substring(decimalPos+1);
		
		//get last pos in dollars text
		int lastPosInDollars = dollars.length()-1;
	
		//get hundreds digit
		char hundredsDigit = dollars.charAt(lastPosInDollars-2);
	
		//get tens digit
		char tensDigit = dollars.charAt(lastPosInDollars-1);
	
		//get ones digit
		char onesDigit = dollars.charAt(lastPosInDollars-0);
		
		//print
		System.out.println ("The ones digit is " + onesDigit);
		System.out.println ("The tens digit is " + tensDigit);
		System.out.println ("The hundreds digit is " + hundredsDigit);
		
	}
}

:?: For more help,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.