1

I want to conver a string to number in Java. I already tried with two methods but both work bad with integers, adding an unneeded floating point: "1" > 1.0 (when I want "1" > 1 and "1.5" > 1.5). I found a couple more ways to convert strings to numbers but they either don't work or are many lines long, I cannot believe it's so complicated coming from javascript where I only need parseFloat().

This is what I'm trying now:

String numString = "1".trim().replaceAll(",","");
float num = (Float.valueOf(numString)).floatValue(); // First try
Double num2 = Double.parseDouble(numString); // Second try
System.out.println(num + " - " + num2); // returns 1.0 - 1.0

How can I have the floating point only when needed?

3
  • 1
    why not use Integer.parseInt(numString);? Commented Dec 29, 2012 at 19:40
  • 1
    @Priyanka.Patil Because the implication is that one of the numbers will be 1.5 (output -> "1.5"). Commented Dec 29, 2012 at 19:45
  • Please think about your question first "How can I have the floating point only when needed?" that's where the problem is and read my answer below. Hint "have" as in "print on screen" or "store in variable"? Commented Dec 29, 2012 at 20:01

3 Answers 3

3

To format a float as you wish, use DecimalFormat :

DecimalFormat df = new DecimalFormat("#.###");
System.out.println(df.format(1.0f)); // prints 1
System.out.println(df.format(1.5f)); // prints 1.5

In your case, you could use

System.out.println(df.format(num) + " - " + df.format(num2));
Sign up to request clarification or add additional context in comments.

Comments

1

I think what you're looking for is DecimalFormat

DecimalFormat format = new DecimalFormat("#.##");
double doubleFromTextField = Double.parseDouble(myField.getText());
System.out.println(format.format(doubleFromTextField));

Comments

0

The problem is with your question really in a type-safe language and I think you are mixing conversion and string representation. In Java or C# or C++ you convert to some predictable/expected type, looks like you expect the "Variant" behavior that you are used to in JavaScript.

What you could do in a type-safe language is this:

public static Object convert(String val)
{
  // try to convert to int and if u could then return Integer
  ELSE
  //try to convert to float and if you could then return it
  ELSE
  //try to convert to double
  etc...
}

Of course this is very inefficient just like JavaScript is compared to C++ or Java. Variants/polymorphism (using Object) comes at cost

Then you could do toString() to get integer formatted as integer, float as float and double as double polymorphically. But your question is ambiguous at best that leads me to believe that there is conceptual problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.