Hey, so I am wondering what would be the easiest way to check if a double "looks" a certain way. Like this:
Let's say I have some input and I want to check if the input has the same format that I want it to be. Here's what I want to achieve;

if (inputdoublevalue == X.XXXX )

So basically if the input would look like this: 5.1234 it would proceed with the if statement, otherwise if the double would be, let's say "05.12" it would not. I don't want it to convert my double either, just check if it has the format I want it to have.

I suspect it may have something to do with DecimalFormat maybe? How can I achieve this?! Thanks!

Recommended Answers

All 3 Replies

A double is a numeric value that doesn't a a "look" until you format it.
How it "looks" depends on how you format it for viewing.
The DecimalFormat class is one way to format a number for viewing.

Are you comparing numeric values or Strings?

What I would like to achieve is that I have a JTextField and I intend to convert the String to a double and store it somewhere. So to answer your question, I suppose I could do it either way, whichever that works, I have not played around very much at all with formatting!

However! The purpose of my question... My intention is to notify "the user" that the input given is not acceptable because the input has to look exactly, let's say like "XX.XX" (example: 11.99). So if the user attempts the number "11.999" or "1.9" it would give the user the error message that it's not acceptable and has to be written as XX.XX.

As I am writing this, I got the idea that perhaps I could solve this with a regular expression / regex api unless there's even a better way? (sorry if double post, NoScript was blocking Daniweb when and it seems that the post wasn't added so I am reposting this post!)

You should be able to test if a numeric value is 11.9 vs 11.99 by comparing the numbers.
If you are testing a String for the format: "dd.dd" then a regular expression would be better.

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.