Java Forum / General / November 2007
re: decimal place checking using JDK1.3
timothy ma and constance lee - 21 Nov 2007 03:56 GMT Dear Sir/Madam
Sir
Thank you for some clue but I am using jdk1.3 so it is not easy to use regular expression,
Please kindly help
I am trying to write a program which check the format:
123.4567899 which is allowed.
The max no. of digit for integer part is 3 and the max of digit after the decimal is 7 so total max length is 11.
It is very strange that when enter 0.45 in the result, it is positive. But it is exception when enter 0.55 - 0.58
public static void main(String[] args) {
Double result = null; //can try from 0.55 - 0.58 wont work
result = Double.valueOf((String)"0.55"); double netResult = 0.00; double netResult1 = 0.00; int decimal = 7; netResult = result.doubleValue() * Math.pow(10,decimal+1); netResult1 = netResult % 1; if (netResult1 >0){ System.out.print("Exception"); } else{ System.out.print("OK"); } System.out.print("\nResult = "+result); System.out.print("\nResult.doubleValue = "+result.doubleValue()); System.out.print("\nnet Result = "+netResult); System.out.print("\nnet Result1 = "+netResult1); }
It is all OK for value except from 0.55 to 0.58 which throw exception, Any clue?
Please kindly help
Arne Vajhøj - 21 Nov 2007 04:06 GMT > Thank you for some clue but I am using jdk1.3 so it is not easy to use > regular expression, The you just have to hack it.
For inspiration:
public static boolean is37digit(String s) { int ix = s.indexOf('.'); if(ix < 1) return false; if(ix > 3) return false; for(int i = 0; i < ix; i++) if(!Character.isDigit(s.charAt(i))) return false; if(s.length() - ix - 1 < 1) return false; if(s.length() - ix - 1 > 7) return false; for(int i = ix + 1; i < s.length(); i++) if(!Character.isDigit(s.charAt(i))) return false; return true; }
Arne
Lew - 21 Nov 2007 04:34 GMT > Please kindly help Unfortunately, when you created a new topic, you fragmented the thread.
 Signature Lew
Andrew Thompson - 21 Nov 2007 05:22 GMT >> Please kindly help > >Unfortunately, when you created a new topic, you fragmented the thread. Would you say that no thread whould ever be reissued with a new title?
It is a vexatious issue, but I myself have recommended to people on occasions to either a) alter the title, or.. b) start an entirely new thread ..to point out important information, as the OP did.
Unfortunately even a change of subject line on an exiting thread might cause two separate threads to appear in some newsclients.
To the OP. One way to help minimise the chance of being ..criticized, pulled up, over the practice is to add a message to the original thread, pointing out that "the title has been changed to '....' because of '....' and will further responders please reply under the new title".
That still might trip some people up, but at least those who see the original thread and read it (or at least scan it all) should be better informed, and can avoid making unnecessary posts to a 'dead' thread.
 Signature Andrew Thompson http://www.physci.org/
Lew - 21 Nov 2007 13:57 GMT Lew wrote:
>> Unfortunately, when you created a new topic, you fragmented the thread.
> To the OP. One way to help minimise the chance of > being ..criticized, pulled up, over the practice is to add a There was no criticism intended. I simply observed that the change caused a fragmentation of the thread, which it did. I never said that that was bad.
 Signature Lew
Lew - 21 Nov 2007 13:58 GMT > Lew wrote: >>> Unfortunately, when you created a new topic, you fragmented the thread. [quoted text clipped - 5 lines] > caused a fragmentation of the thread, which it did. I never said that > that was bad. Morally bad = I did say "unfortunate", but that's not a moral judgment.
 Signature Lew
Andrew Thompson - 21 Nov 2007 14:22 GMT >> Lew wrote: >>>> Unfortunately, when you created a new topic, you fragmented the thread. ...
>> caused a fragmentation of the thread, which it did. I never said that >> that was bad. > >Morally bad = I did say "unfortunate", but that's not a moral judgment. (red face) *My* bad**. (backtracking quickly)..English is a f*cking complex and subtle language, no?(/backtracking quickly)
** Interpret that meaning of 'bad', as you wish, I did not intend any subtlety, in my use of it.
 Signature Andrew Thompson http://www.physci.org/
Chris ( Val ) - 21 Nov 2007 14:44 GMT On Nov 21, 2:56 pm, "timothy ma and constance lee" <timco...@shaw.ca> wrote:
> Dear Sir/Madam > [quoted text clipped - 42 lines] > It is all OK for value except from 0.55 to 0.58 which throw exception, > Any clue? You can use something like the following code as a starting point.
You might like to add additional parameters that accept a value constraint for the number of digits before and after the decimal point, or even create these methods in a separate class altogether - It's up to you to modify it to suit your needs.
I have created some basic test cases, so you can run it to see the results.
public class CheckNumberFormat { public static boolean isParseable( String src ) { Double.parseDouble( src );
return true; }
public static boolean isValidFormat( String src ) { final int NOT_FOUND = -1;
if( !isParseable( src ) || src.indexOf( '.' ) == NOT_FOUND ) return false;
return src.indexOf( '.' ) < 4 && src.length() - (src.indexOf( '.' ) + 1) < 8; }
public static void main( String[] args ) { String[] numArray = { "123", "123.4567890 ", ".2", "0.1", "0.0", "1.2", "1234.1", "0.55", "0.12345678", "." }; try { for( String token : numArray ) { if( isValidFormat( token.trim() ) ) System.out.println( "Passed: " + token ); else System.out.println( "\t\t\tRejected: " + token ); } } catch( NumberFormatException e ) { System.out.println( "Could not parse token: " + e.getMessage() ); } } }
-- Chris
Patricia Shanahan - 21 Nov 2007 14:52 GMT ...
> public static boolean isParseable( String src ) { > Double.parseDouble( src ); > > return true; > } ...
The OP needs to decide whether to accept numbers like "1.3e100". Java's built-in double parsing allows the "e" notation.
If such numbers are acceptable, then I believe the testing needs to take them into account. If not, the check for whether the number can be parsed needs to exclude them.
Patricia
Chris ( Val ) - 21 Nov 2007 15:09 GMT > ...> public static boolean isParseable( String src ) { > > Double.parseDouble( src ); [quoted text clipped - 10 lines] > them into account. If not, the check for whether the number can be > parsed needs to exclude them. Hi Patricia,
Your concerns are quite valid, and the OP should decide if such numbers are acceptable. It was just provided as a starting point, that's why I said it is up to the OP to modify it to suit :-)
-- Chris
Patricia Shanahan - 21 Nov 2007 16:03 GMT >> ...> public static boolean isParseable( String src ) { >>> Double.parseDouble( src ); [quoted text clipped - 15 lines] > provided as a starting point, that's why I said it > is up to the OP to modify it to suit :-) Understood. I was just concerned that the OP might not think of the "e" issue in doing the modification.
Patricia
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|