Friday, August 12, 2022

[FIXED] How do i validate it so that the first 3 digits range 100-300(inclusive) and the second 3 digits range 001-999(inclusive) in the following

Issue

private boolean validateSno(double inSno) {
  //sno is the serial number


  int firstThree=(int)inSno;
  double secondThree=(inSno-firstThree)*1000;
  boolean same=false;
  double checkDigit=inSno*1000-(int)inSno;

  if(checkDigit>0.0)
  {
  same=false;
  }
  else 
  {
      if(firstThree>=100&&firstThree<=300)
      {
      if(secondThree>=001&&secondThree<=999)
      {
          same=true;
      }

      }
  }
  return same;
}

I need the result to be in form XXX.YYY but as it is a real number I am stuck on how to split the first 3 numbers and the second 3 numbers. Each has its own validation required

is there an easy way using mod/div or?


Solution

For example number 456.234

double number = 456.234;
double firstThree = (int)number; \\ output 456
double lastThree = (number * 1000 % 1000); \\ output 234


Answered By - MrHolal
Answer Checked By - Gilberto Lyons (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.