Check nbo decimal values of double

Hi there!
I need some advice on a topic:
What is the best practice to check the number of decimal places, double values have (e.g. two decimal places are allowed, when there are more an exception has to be thrown)?
I did some research, but could not find a truely satisfying solution.
Hope you can help!
greetz

YoungWinston wrote:
JoachimSauer wrote:
You could try to get the String representation using String.valueOf() and check how many characters it contains after the decimal point. If the double value is really the result of the parsing of that String value, then the result should not have more digits than the original input...Are you sure that every possible value created by a string with up to 2 decimal places will convert back to the same string Joachim? It's possible, but I've never seen any confirmation of it.I'm not 100% sure, but I seem to remember some documentation to that effect. I was just too lazy to look it up ... I'm looking into it now.
I think I found it:
[Double.toString(double)|http://java.sun.com/javase/6/docs/api/java/lang/Double.html#toString(double)] says:
How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double.(emphasis mine)
If my understanding is correct, then this rule ensures that the shortest possible String literal that parses into the same double value must be produced. So if the String literal "0.2" parses to the double value that you have, then Double.toString() must not produce 0.20, because that's more digits than would be necessary.
Of course this approach breaks down if you exceed the possible precision of the double (i.e. if you have 15 digits before the decimal point, then anything after the decimal point will be lost/effectively random).

Similar Messages

  • How to check a decimal value is negative or not.

    Hi,
    Iam having a value 25000.00- in a variable called sum1.
    which is a negative value.
    i want to check whether it is a negative value or not.
    i tried like
    if sum1 LT 0.
    statement.
    else.
    statement.
    but this is not working, can anyone help me in this.
    Thanks,
    Rose.

    Hi,
    DATA: sum TYPE p DECIMALS 2,
               v_len TYPE I,
               v_cnt1 TYPE I,
               v_cnt2 TYPE I.
      sum = '25000.00-'.
    v_len = strlen( sum ).
    DO v_len TIMES.
      v_cnt2 =  v_cnt2 + 1.
       IF sum+v_cnt1(v_cnt2) EQ '-'.      
         WRITE : 'Have  Negative sign'.
      ENDIF.
        v_cnt1  = v_cnt1 + 1.  
    ENDDO.

  • How to check decimal value in numeric field

    Is there any way to find number of decimal digit is being used.
    999.444
    444.45
    344.23
    33.666666666666
    Query provide rusult after the decimal value.
    thanks.

    user-Keen wrote:
    Query provide rusult after the decimal value.
    thanks.
    your_col - trunc(your_col)Do you have negative numbers?
    For negative values what do you want to display?
    If just the decimal part, you could use ABS(col) - trunc(ABS(col))
    Edited by: jeneesh on Apr 4, 2013 3:29 PM

  • Decimal values check

    Hi,
    Given a string of character values, I get the values comprising of character values and also floating point values. eg 25.75 . The problem is the decimal value is not deciphered as a decimal but instead a character value. Could you please give me the name of the function module that would recognise the floating point value..
    Thanks in advance....

    Hi Sanjay,
    You will have to parse the character variable character by character to determine if it contains any character field.
    This can easily be achieved by the following code.
    REPORT ZTMP.
    DATA: WA_TMP TYPE P DECIMALS 2,
          WA_TXT(10) VALUE '35.56',
          WA_LTH TYPE I,
          WA_INDEX TYPE I VALUE 0,
          WA_INDC(1) VALUE ' '.
    CONDENSE WA_TXT.
    WA_LTH = STRLEN( WA_TXT ).
    DO WA_LTH TIMES.
      IF WA_TXT+WA_INDEX(1) CO '0123456789.'.
        WA_INDC = 'X'.
      ELSE.
        WA_INDC = ' '.
        EXIT.
      ENDIF.
      WA_INDEX = WA_INDEX + 1.
    ENDDO.
    IF WA_INDC EQ ' '.
      WRITE: 'VARIABLE CONTAINS A STRING.'.
    ELSE.
      WRITE: 'VARIABLE CONTAINS A PACKED DECIMAL.'.
    ENDIF.
    Using CO directly will not work.
    Regards,
    Madhur
    CORRECTION ***************
    Sorry Sanjay,
    I made a mistake here. Using CO directly will work if we include a SPACE " " character in our CO criteria. This would be as follows:
    REPORT ZTMP .
    DATA: WA_TMP TYPE P DECIMALS 2,
          WA_TXT(10) VALUE '35.57',
          WA_LTH TYPE I,
          WA_INDEX TYPE I VALUE 0,
          WA_INDC(1) VALUE ' '.
    CONDENSE WA_TXT.
    IF WA_TXT CO '0123456789. '. "This contains a space char.
      WRITE: 'VARIABLE CONTAINS A PACKED DECIMAL.'.
    ELSE.
      WRITE: 'VARIABLE CONTAINS A STRING.'.
    ENDIF.
    Message was edited by: Madhur Chopra
    Message was edited by: Madhur Chopra

  • Checking for null value in arraylist

    Hi
    i have an excel file which i i am reading into an arraylist row by row but not necesarrily that all columns in the row mite be filled. So how do i check for null values in the array list.
    try
                        int cellCount = 0;
                        int emptyRow = 0;
                        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
                        HSSFSheet sheet = workbook.getSheetAt(0);
                        Iterator rows = sheet.rowIterator(); 
                        myRow = new ArrayList();
                        int r = 1;
                             while (rows.hasNext())
                                  System.out.println("Row # " + r);
                                  HSSFRow row = (HSSFRow) rows.next();
                                  Iterator cells = row.cellIterator();          
                                  cellCount = 0;
                                  boolean isValid = false;
                                  while (cells.hasNext())
                                       HSSFCell cell = (HSSFCell) cells.next();
                                       switch (cell.getCellType())
                                            case HSSFCell.CELL_TYPE_NUMERIC:
                                                 double num = cell.getNumericCellValue();     
                                                 DecimalFormat pattern = new DecimalFormat("###,###,###,###");     
                                                 NumberFormat testNumberFormat = NumberFormat.getNumberInstance();
                                                 String mob = testNumberFormat.format(num);               
                                                 Number n = null;
                                                 try
                                                      n = pattern.parse(mob);
                                                 catch ( ParseException e )
                                                      e.printStackTrace();
                                                 System.out.println(n);
                                                 myRow.add(n);                                             
                                                 //myRow.add(String.valueOf(cell.getNumericCellValue()).trim());
                                                 //System.out.println("numeric: " +cell.getNumericCellValue());
                                                 break;
                                            case HSSFCell.CELL_TYPE_STRING:
                                                 myRow.add(cell.getStringCellValue().trim());
                                                 System.out.println("string: " + cell.getStringCellValue().trim());
                                                 break;
                                            case HSSFCell.CELL_TYPE_BLANK:
                                                 myRow.add(" ");
                                                 System.out.println("add empty:");
                                                 break;
                                       } // end switch
                                       cellCount++;
                                  } // end while                    
                                  r++;
                             }// end while
                   } myRow is the arrayList i am adding the cells of the excel file to. I have checked for blank spaces in my coding so please help with how to check for the black spaces that has been added to my arraylist.
    I have tried checking by looping through the ArrayList and then checking for null values like this
    if(myRow.get(i)!=null)
      // do something
    // i have tried this also
    if(myRow.get(i)!="")
    //do something
    }Edited by: nb123 on Feb 3, 2008 11:23 PM

    From your post I see you are using a 3rd party package to access the Excel SpreadSheets, you will have to look in your API for you 3rd party package and see if there is a method that will identify a blank row, if there is and it does not work, then you have to take that problem up with them. I know this is a pain, but it is the price we pay for 3rd party object use.
    In the mean time, you can make a workaround by checking every column in your row and seeing if it is null, or perhaps even better: check and see if the trimmed value of each cell has a lenth of 0.

  • Decimal  values for a field in ALV

    Hi,
    I created an ALV report.
    It has a field which will display the calculated value as '103,839,389'. This should be dispalyed as '103,839,389.00' with decimal value, I mean with 2 decimal places, how can this be achieved?
    Thanks,
    Kumar

    hi check this..
    use these in populating the fieldcat..
    it_fieldcat-decimalsfieldname = fieldname
    it_fieldcat-decimalstabname   = tabname
    it_fieldcat- decimals_out   = 2.
    regards,
    venkat

  • Loading decimal  value to an infoobject of type char

    I have an extract structure with field timestamp, which is of type dec and length 15. Now i want to load this value to an infoobject in ODS. But this infoobject is of type char and length 18. when infopackage loading is scheduled it is giving the following error. Record 1 :InfoObject /BIC/IFITMSTMP does not contain alpa-conforming value 20061206084944.
    Is it because i tried to move a dec valu into a char field with a different length? If so  how will i load a decimal value of length 15 to a char of length 18. Can anyone please help me with this?
    Thanks in advance.

    if u r using BI7.0
    go to the datasource of the cube
    in the general info tab of the datasource
    there is a check box with PSA not typed
    check it
    if u check it all the values from the external source will be converted into character format irrespective of their data type

  • Loading a decimal value to an infoobject of type char

    I have an extract structure with field timestamp, which is of type dec and length 15. Now i want to load this value to an infoobject in ODS. But this infoobject is of type char and length 18. when infopackage loading is scheduled it is giving the following error. Record 1 :InfoObject /BIC/IFITMSTMP does not contain alpa-conforming value 20061206084944.
    Is it because i tried to move a dec valu into a char field with a different length? If so  how will i load a decimal value of length 15 to a char of length 18. Can anyone please help me with this?
    Thanks in advance.

    if u r using BI7.0
    go to the datasource of the cube
    in the general info tab of the datasource
    there is a check box with PSA not typed
    check it
    if u check it all the values from the external source will be converted into character format irrespective of their data type

  • Error saving purchase order data decimal values in SAP R/3

    sir,
    PO was created with currency japanese YEN using Tcode ME21N . The net price was showing 142502300 (in JPY).
    when checked in EKPO table , it saved with a value 1425023.00  instead of  142502300 . 
    what can be done to correct this.      
    In PO Print out , it showing values as 1425023.00  which is wrong.
    SPRO>> NET WEAVER >> GENERAL SETTINGS >> CURRENCY>> SET DECIMAL values FOR CURRENCY CODES is showing 0 decimals against JPY (Japanese YEN)
    I want to know where else the Decimal values could have been set to 2 decimal places.
    FOr other currencies like US Dollars  there is no error.
    Regards,
    Srinivasa Murthy

    Hello Srinivasa
    Please check the forum Rules of Engagement - https://wiki.sdn.sap.com/wiki/display/HOME/RulesofEngagement.
    Cross posting is not permitted. This issue is already raised in the application area forum which is probably correct. This does not look like a PI technical issue.
    Regards
    Mark Smyth
    XI/PI Moderator

  • Purchase order with decimal values in held status.

    Hello, i have a SC. The quantity is decimal for the line item.When it was approved, the PO got a 'held' status.How do i release the PO and what can i do in order to accept decimal values in my SC ?

    Hello,
    Are you sure this is not a workflow issue?  If it is held, someone either placed it in hold status or something like that.  Since, there was nothing to suspect an application error, I don't think the decimals is your issue.  If you feel that it is, have you checked OSS.
    More information would be helpful....
    Regards, Dean.

  • Error saving purchase order data decimal values

    sir,
    PO was created with currency japanese YEN using Tcode ME21N . The net price was showing 142502300 (in JPY).
    when checked in EKPO table , it saved with a value 1425023.00  instead of  142502300 . 
    what can be done to correct this.      
    In PO Print out , it showing values as 1425023.00  which is wrong.
    SPRO>> NET WEAVER >> GENERAL SETTINGS >> CURRENCY>> SET DECIMAL values FOR CURRENCY CODES is showing 0 decimals against JPY (Japanese YEN)
    I want to know where else the Decimal values could have been set to 2 decimal places.
    FOr other currencies like US Dollars  there is no error.
    Regards,
    Srinivasa Murthy

    Hi Srinivasa,
    Use T. code OY01 and you can try 3rd option of decimal format that is 1234567, 89 for the same.
    Regards,
    Shailesh Verma

  • Problem with decimal values

    Hi
    I need to store a very long decimal value. something like
    0.123456789012 in to the SQL data base.
    However after doing some calculations , my value is trim to
    0.12345
    float value = v1 v2v3;
    v1 is long and v2 is a float and v3 is in double.
    anyone noes y? I need the value to be in full lenght (as in all the numbers after the decimal point).
    many thanks !
    yan

    but my SQL database is designed to store float
    value.
    I haf no control of the SQL database..Don't assume that float in the SQL database is the same as float in Java code.
    SQL-Server 2000's float is the same as a Java double. Depending on the RDBMS you are using, the same words may have a different meaning between the RDBMS and Java

  • Decimal values for EA in MIGO

    Dear Experts
    Recently we have upgraded to SAP AFS 6.5 from SAP AFS 5.0. In previous version we could GR (using MIGO) decimal values for unit of measure EA. Now when we are trying to perform goods receipt decimal values it is automatically rounding off to an integer.
    After go live we got an issue which is similar like this and it was corrected after consulting SAP OSS. check the this; Rounding off in MIGO- Error
    I have checked settings in CUNI in previous and current versions. There is no change. Decimal places are zero. Please suggest if you have any idea on this.
    Regards
    Jeewana

    Hi,
       Please check whether all the correction instructions mentioned in the note:  1971912 
    are implemented correctly. If its already maintained correctly, then you may directly raise an OSS ticket to SAP.
       Also refer the similar notes: 1893043 - Decimal places not allowed in MIGO for movement type 561
    1932640 - Incorrect rounding in asset PO with no material.
    Regards,
    AKPT

  • Values are doubled in analyzer??? why???

    I have created this query:
    free characteristics: calendar day/sold to party/ material
    key figures:billed quantity/billing value
    when I run this query in analyzer I get the exact double values than the ones I should get. I mean in R3 the correct values that I check from another report there are halpf, which is the correct. In BW analyzer the values are doubled
    Why does this happen???
    I also have anotheer problem, I can get values up to 07/2008? the job probably has an error ever since and does not run automatically?? any ideas?? where should I look?(transactions)
    Thank you

    Hi,
    when I run this query in analyzer I get the exact double values than the ones I should get. I mean in R3 the correct values that I check from another report there are halpf, which is the correct. In BW analyzer the values are doubled
    Why does this happen???
    --> First check data in infocube, i think data loaded wrongly, hence double records. check for particular material or customer for a month.
    I also have anotheer problem, I can get values up to 07/2008? the job probably has an error ever since and does not run automatically?? any ideas?? where should I look?(transactions)
    --> Please check your other post
    Hope it Helps
    Srini

  • Decimal values not showing in PO output

    In PO qty is created with decimal values ( 30.500 M)
    when print  the output it is coming as 31 m ( values is getting rounded off)
    PO is created with ref to sales order , where as qty is  EA
    in SO . ( Qty EA is not affecting PO / PR)
    Please advise on the PO configuration part to get the PO output with decimal values.
    Thanks & regards
    Ray

    if PO has the correct quantity with decimal places, then check the sapscript form.
    Muralidhar

Maybe you are looking for