i have got a program to convert a double value to a string.
eg==INPUT= 3.65
OUTPUT=three point six five.
can anyone give the solution.

Dani AI

Generated

wanted 3.65 -> "three point six five". As pointed out, the straightforward, reliable way is to treat the numeric value as a string and map each character to a spoken word. That avoids re-implementing full English-number grammar and matches the example (digit-by-digit after the decimal point).

A simple approach:

  • Get a stable decimal string (use BigDecimal.valueOf(double).toPlainString() or a DecimalFormat pattern if you need fixed decimal places).
  • Walk the string and map '0'..'9' to words, '.' to "point" (or "dot"), and '-' to "minus".
  • If you need the integer part read as natural words ("twelve" instead of "one two"), add a separate routine; that is a different problem.

Example Java implementation (compact, digit-by-digit):

private static final String[] DIGITS = {
  "zero","one","two","three","four","five","six","seven","eight","nine"
};

public static String numberToSpoken(double value, Integer fixedDecimals) {
  String s;
  if (fixedDecimals != null) {
    StringBuilder pat = new StringBuilder("0.");
    for (int i = 0; i < fixedDecimals; i++) pat.append('0');
    s = new java.text.DecimalFormat(pat.toString()).format(value);
  } else {
    s = java.math.BigDecimal.valueOf(value).toPlainString();
  }

  StringBuilder out = new StringBuilder();
  for (char c : s.toCharArray()) {
    if (c == '-') out.append("minus ");
    else if (c == '.') out.append("point ");
    else if (Character.isDigit(c)) out.append(DIGITS[c - '0']).append(' ');
  }
  return out.toString().trim();
}

Notes and pitfalls: doubles can lose the original number of decimal places (3.50 becomes 3.5), so if preserving trailing zeros matters, accept the input as a String or use a DecimalFormat pattern. For accurate monetary or exact decimal work, prefer BigDecimal input. If the goal is full English number names (hundreds/thousands), look for a number-to-words routine and apply it only to the integer part. Also heed ’s forum reminder about showing effort when the code is homework.

Recommended Answers

All 3 Replies

I guess you missed this on the main forum page:

don't expect quick solutions to your homework. We'll help you get started and exchange algorithm ideas, but only if you show that you're willing to put in effort as well

Member Avatar for Member #814414

You can't convert 3 to 'three' directly. You need to write a method to examine each character and return an appropriate value.

e.g.
if(input is equal to 1)
return [appropriate String]

Then take the returned values and print them.

thankyou coil for the answer

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.