Another problem about decimal numbers

hi,
i have a code and inside my code, i declared a variable called 'subtotal' as a double. This variable is to hold the amount of money to be paid for a particular type of item. The part of code is as below:
double subtotal = 0.00, price;
int quantity;
(statements)
subtotal = price * quantity;Then i tested my code. But i found out that, if i entered a price, for example, $2.3, and 3 for quantity, instead of having a value of $6.9 for my subtotal, the value became $6.8888888889. Why is that so? How to solve this problem?
Thanks for help!

@OP: as you might know by now: not every decimal finite fraction can be represented
exactly by a finite binary fraction. Computers represent fractional numbers as a
finite series of negative powers of two, i.e. 1/2, 1/4, 1/8, 1/16 and so on. It only
can store a finite number of those negative powers of two in a 'double' value.
Think of it this way: there are negative powers of two on the table (1/2, 1/4, 1/8 ...)
and you've given the opportunity to pick any combination of them so you can add
them up and equal 0.1 (decimal 1/10). You're only allowed to pick up any number
at most once; here we go:
You pick 1/16 because 1/8 is too big and 1/32 doesn't make sense. You now
have to reach the number 1/10-1/16= 3/80 in order to close the gap.
You pick 1/32 because 1/16 is too big and 1/64 doesn't make sense. You now
have to reach the number 3/80-1/32=1/160 in order to close the gap.
You pick 1/256 because 1/128 is too big and 1/512 doesn't make sense. You now
have to reach the number 1/160-1/256=3/1280 in order to close the gap.
You pick 1/512 because 1/256 is too big and 1/1024 doesn't make sense. You now
have to reach the number 3/1280-1/512=1/2560 to close the gap.
etc.etc. etc.
You end up picking the numbers sum(i= 1, ...)(1/2^(4*i)+1/2^(4*i+1)) and you
have to stop after, say, 64 tries and you never reach 1/10 (decimal) exactly.
This is what that FPU has to do and it can't reach the number 1/10 exactly
either, i.e. it picks the number closest to it.
There are an infinit amount of decimal fractions that can not be represented
as a finite sum of negative powers of two. The opposite is true also.
Learn to live with that truth and let a DecimalFormat pick the correct rounding
value for you, it makes life much easier, because you only want to represent
the 'correct' values (mind the quotes).
kind regards,
Jos

Similar Messages

  • Problem with decimal numbers..

    hi i posted the other day with a problem I had. I thought it was all sorted but I have just realised that there is another problem.
    I have the following code for an applet:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TextFieldExample extends Applet implements ActionListener {
    TextField textInput;
    String text = "";
    public void init() {
    textInput = new TextField (20);
    add(textInput);
    textInput.addActionListener(this);
    public void actionPerformed(ActionEvent e) {
    text = textInput.getText();
    repaint();
    public void paint(Graphics g) {
         int x = Integer.parseInt(text);
         double y = Math.abs(x);
    g.drawString("You wrote " + text,20,500);
         g.drawString("The absolute value is: " + y,20,100);
    I am trying to do the applet so when you type in an angle, it tell you the sine, cosine etc. I can get it to work with whole numbers like 1,10,300, but it won't work when i try to use decimal numbers. Can anyone help?
    Thnk you in advance!
         

    Hi,
    you are using Integer.parseInt() to convert the input, so it will only work if the text is an integer. Use Double.parseDouble() instead.
    Andre

  • Another problem about datatable

    Hi ,
    I found that default value of seletItem is not selected in dataTable.
    Here is a example:
    <h:dataTable value="#{users}" var="user1">
       <h:column>
            <h:selectOneListbox value="1"
               <f:selectItem itemValue="1" itemLabel="c1"/>
               <f:selectItem itemValue="2" itemLabel="c2"/>
               <f:selectItem itemValue="3" itemLabel="c3"/>
          </h:selectOneListbox>
      </h:column>
    </h:dataTable>
    The default value c1 should be selected, but it isn't .
    Could someone kindly tell me why and how should I do?
    Many thanks in advance.
    Michael

    try:
    <h:dataTable value="#{users}" var="user1">
       <h:column>
            <h:selectOneListbox value="c1"
               ><f:selectItem itemValue="1" itemLabel="c1"/>
               <f:selectItem itemValue="2" itemLabel="c2"/>
               <f:selectItem itemValue="3" itemLabel="c3"/>
          </h:selectOneListbox>
      </h:column>
    </h:dataTable>

  • Problem about space management of archived log files

    Dear friends,
    I have a problem about space management of archived log files.
    my database is Oracle 10g release 1 running in archivelog mode. I use OEM(web based) to config all the backup and recovery settings.
    I config "Flash Recovery Area" to do backup and recovery automatically. my daily backup schedule is every night at 2:00am. and my backup setting is "disk settings"--"compressed backup set". the following is the RMAN script:
    Daily Script:
    run {
    allocate channel oem_disk_backup device type disk;
    recover copy of database with tag 'ORA$OEM_LEVEL_0';
    backup incremental level 1 cumulative copies=1 for recover of copy with tag 'ORA$OEM_LEVEL_0' database;
    the retention policy is the second choice, that is "Retain backups that are necessary for a recovery to any time within the specified number of days (point-in-time recovery)". the recovery window is 1 day.
    I assign enough space for flash recovery area. my database size is about 2G. I assign 20G as flash recovery area.
    now here is the problem, through oracle online manual, it said oracle can manage the flash recovery area automatically, that is, when the space is full, it can delete the obsolete archived log files. but in fact, it never works! whenever the space is full, the database will hang up! besides, the status of archived log files is very strange, for example, it can change "obsolete" stauts from "yes" to "no", and then from "no" to "yes". I really have no idea about this! even though I know oracle usually keep archived files for some longer days than retention policy, but I really don't know why the obsolete status can change automatically. although I can write a schedule job to delete obsolete archived files every day, but I just want to know the reason. my goal is to backup all the files on disk and let oracle automatically manage them.
    also, there is another problem about archive mode. I have two oracle 10g databases(release one), the size of db1 is more than 20G, the size of db2 is about 2G. both of them have the same backup and recovery policy, except I assign more flash recovery area for db1. both of them are on archive mode. both of nearly nobody access except for the schedule backup job and sometime I will admin through oem. the strange thing is that the number of archived log files of smaller database, db2, are much bigger than ones of bigger database. also the same situation for the size of the flashback logs for point-in-time recovery. (I enable flashback logging for fast database point-in-time recovery, the flashback retention time is 24 hours.) I found the memory utility of smaller database is higher than bigger database. nearly all the time the smaller database's memory utility keeps more than 99%. while the bigger one's memory utility keeps about 97%. (I enable "Automatic Shared Memory Management" on both databases.) but both database's cup and queue are very low. I'm nearly sure no one hack the databases. so I really have no idea why the same backup and recovery policy will result so different result, especially the smaller one produces more redo logs than bigger one. does there anyone happen to know the reason or how should I do to check the reason?
    by the way, I found web based OEM can't reflect the correct database status when the database shutdown abnormally. for example, if the database hang up because of out of flash recovery area, after I assign more flash recovery area space and then restart the database, the OEM usually can't reflect the correct database status. I must restart OEM manually to correctly reflect the current database status. does there anyone know in what situation I should restart OEM to reflect the correct database status?
    sorry for the long message, I just want to describe in details to easy diagnosis.
    any hint will be greatly appreciated!
    Sammy

    thank you very much, in fact, my site's oracle never works about managing archive files automatically although I have tried all my best. at last, I made a job running daily to check the archive files and delete them.
    thanks again.

  • Problem with importing decimal numbers in Apex

    Hi,
    I have a ascii data file which I load into db tables calling a procedure in a db package.
    Executing the procedure by SQL Plus everything works fine.
    Calling the procedure by Apex button I get a "ORA-01722: invalid number; Error inserting subject" because of the decimal numbers in the data file. Taking the decimal numbers out of the file it works.
    - I tried with different formats in the data file like: 4.40 4,40 4.4 4,4: Makes no difference.
    - In Apex I tried column formatting in the report attribute: 999G999G999G999G990D00: Makes no difference, it's obviously only formatting the display.
    - Debug mode just shows there is an error..
    - The db column is of format NUMBER(10,2)
    As I mentioned, with SQL Plus it works no matter of the number format in the data file.
    Anybody has an idea where the problem might be?
    Help is greatly appreciated,
    Roger

    Hi Joel,
    Thanks for your reply. You were right, the NLS setting for the decimal separator in SQL*Plus is a , (komma) and in Application Express a . (point).
    I suspected a NLS problem that's why I tested with different formats in the data file (4.40 4,40). Shouldn't that make a difference?
    Now, to fix it: I didn't see in Apex any NLS settings, I checked in utilities and shared components. So, the way to do that is to directly write into SQL Workshop / Apex?
    ALTER SESSION SET NLS_NUMERIC_CHARACTERS=",' "
    Cheers,
    Roger

  • I have an ipod 5 (IOS8) and two days ago my ipod died and when I tried to on it after charging it would turn on to the apple symbol and after about 5 minutes it would shut off. Another problem is that i also have a jammed home button.

    I have an ipod 5 (IOS8) and two days ago my ipod died and when I tried to on it after charging it would turn on to the apple symbol and after about 5 minutes it would shut off. Another problem is that i also have a jammed home button. Is there anyway i can fix my ipod?

    Try:
    Turn on without Home/Power Button

  • Sql Loader - Decimal numbers showing in null column

    Greetings,
    My apologies if this is in the wrong forum section. It seemed to be the most logical.
    I have added new column to a control file used in a sql loader upload and I am getting unexpected results. Long story short, I copy foxpro tables from a network directory to my local pc. A foxpro exe converts these tables to .dat files. Sql loader then uploads the .dat files to matching oracle tables. I've run this program from my pc for years with no problems.
    Problem now: We added a new column to a foxpro table and to the matching oracle table. This column in FoxPro in null for now - no data at all. I then added the new column to my ctl file for this table. The program runs, sql loader does it's thing with no errors. However, in the new field in Oracle, I'm finding decimal numbers in many of the records, when all records should have null values in this field. I've checked all other columns in the oracle table and the data looks accurate. I'm not sure why I'm getting these decimal values in the new column.
    My log and bad files show no hints of any problems. The bad file is empty for this table.
    At first I thought the positioning of the new column in the fox table, .ctl file and the oracle table were not lining up correctly, but I checked and they are.
    I've double checked the FoxPro table and all records for this new column are null.
    I'm not sure what to check for next or what to test. I am hoping someone in this forum might lend a clue or has maybe seen this problem before. Below is my control file. The new column is the last one: fromweb_id. It is a number field in both FoxPro and Oracle.
    Thanks for any advise.
    JOBS table control file:
    load data
    infile 'convdata\fp_ora\JOBS.dat' "str X'08'"
    into table JOBS
    fields terminated by X'07'
    TRAILING NULLCOLS
    (SID,
    CO_NAME "replace(replace(:CO_NAME,chr(11),chr(10)),chr(15),chr(13))",
    JOB_TITLE "replace(replace(:JOB_TITLE,chr(11),chr(10)),chr(15),chr(13))",
    CREDITS,
    EARN_DATE date "mm/dd/yyyy",
    COMMENTS CHAR(2000) "replace(replace(:COMMENTS,chr(11),chr(10)),chr(15),chr(13))",
    DONT_SHOW,
    PC_SRC "replace(replace(:PC_SRC,chr(11),chr(10)),chr(15),chr(13))",
    PC_SRC_NO,
    SALARY,
    SALFOR,
    ROOM,
    BOARD,
    TIPS,
    UPD_DATE date "mm/dd/yyyy hh12:mi:ss am",
    STUKEY,
    JOBKEY,
    JO_COKEY,
    JO_CNKEY,
    JO_ZUKEY,
    EMPLID,
    CN_NAME "replace(replace(:CN_NAME,chr(11),chr(10)),chr(15),chr(13))",
    JOB_START date "mm/dd/yyyy",
    JOB_END date "mm/dd/yyyy",
    FROMWEB_ID)

    I apologize for not explaining how this was resolved. Sql Loader was working as it should.
    The problem was due to new fields being added to the FoxPro table, along with the fromweb_id column, that I was not informed about. I was asked to add a column named fromweb_id to the oracle jobs table and to the sql-loader program. I was not told that there were other columns added at the same time. In the foxpro table, the fromweb_id column was the last column added.
    The jobs.dat file contained data from all columns in the foxpro table, including all the new columns. I only added the "fromweb_id" to the control file, which is what I was asked to do. When it ran, it was getting values from one of the new columns and the values were being uploaded into the fromweb_id column in Oracle. It is that simple.
    When I had checked the FoxPro table earlier, I did not pickup on the other new columns. I was focussing in on looking for values in the fromweb_id column. When back-tracing data in the jobs.dat file, I found a value in the fromweb_id column that matched a value in a differnt column (new column) in FoxPro. That is when I realized the other new columns. I instantly knew what the problem was.
    Thanks for all the feedback. I'm sorry if this was an inconvenience to anyone. I'll try to dig a little deeper next time. Lessons learned...
    regards,

  • Decimal Numbers in Oracle Configurator

    We are trying to use decimal numbers in a parent Option Class, such the example below:
    Item Type Qty
    - 000000x Option Class 0,14444
    - 00000xx Option Class 1
    What occurs:
    When I used decimal numbers on an option class, the structure down of it and the same level of it doesn´t updating on Oracle Configurator.
    There are any error message. But seems that the structure of this point to down is being ignored.
    Today I have ~5,000 items that use the same structure on my BOM that would need to use this funcionality because the unit of measure is liter, and these items have different quantities of consume and all of them can be configured on Oracle Configurator.
    E.g (According the width of the product that were configured, the quantity of paint necessary to build this product can be variable )
    I don´t know if it´s work because i know that OM have some limitations about, but maybe somebody can help me in this issue.
    Tks!

    Have you found any solutions on this yet?
    I have a similar problem and have an SR open at the mo for it! I am wanting to feed a decimal value out of the model and assign it against the (selected) child of the (selected) option class.
    I have learnt to far that inventory is limited to 4 decimal places so its not going to work if you go over this number.
    Then i learnt that an option class will nto support decimal places at all so you need to feed it directly to the child/ren and skip the option class. To do this I set up a Total feature and in my calculation rule I contributed the output to the Total feature. Then the total feature sends the output value to the child by the rule:
    Contribute 'Dummy' * &x1.state() To &x1 FOR ALL &x1 IN {'MJ OUTER MAT OC'.Options()};
    This isn't the correct solution but its nearly there. This assigns the value to all the children of the OC and not just the one that is required causing the whole model to fall over. If you have any solution to your issue or anyone can help me with this one, would be much appreciated.
    Vikki

  • The problem about  integrate  Portal and R/3

    Hi everyone :
       We want to achieve that our vendor can query R/3 report via our Portal. I had done SSO configuration.
       But we had about 500 vendors, it is impossible that we create 500 R/3 users for our vendors,  because the cost is too much .
       And there is another problem, vendor who had the authorization to query report can query the other vendor's data at same time. But we expect that certain vendor can query his data only.
       I think this is a general problem when integrate Portal and R/3, BW .
       Is there somebody had solved this problem or give any advice?
       Any discuss is welcome.
    Best Regards,
    Jianguo Chen

    Hi,
    I would say: get in contact with your SAP account manager anc check which options SAP can offer you...
    Normally every user using a R/3 system has to have a valid user license in that system. Expecially when you want to access control to data on user (vendor) level you nedd to identifiy the user clearly and uniquely which by standard means you need a user for every vendor.
    Hth,
    Michael

  • The problem about  integrate  Portal and R/3 : vendor query report problem

    Hi everyone :
       We want to achieve that our vendor can query R/3 report via our Portal. I had done SSO configuration.
       But we had about 500 vendors, it is impossible that we create 500 R/3 users for our vendors,  because the cost is too much .
       And there is another problem, vendor who had the authorization to query report can query the other vendor's data at same time. But we expect that certain vendor can query his data only.
       I think this is a general problem when integrate Portal and R/3, BW .
       Is there somebody had solved this problem or give any advice?
       Any discuss is welcome.
    Best Regards,
    Jianguo Chen

    Hi,
    I would say: get in contact with your SAP account manager anc check which options SAP can offer you...
    Normally every user using a R/3 system has to have a valid user license in that system. Expecially when you want to access control to data on user (vendor) level you nedd to identifiy the user clearly and uniquely which by standard means you need a user for every vendor.
    Hth,
    Michael

  • An old and difficult problem about "UnsatisfiedLinkError"

    Hi dear all,
    I have been struck with the problem about "UnsatisfiedLinkError". I have a c++ class HelloWorld with a method hello(), and I want to call it from within a java class. In fact, I have succeeded in calling it on the windows platform. But when I transfer it to linux, the error "UnsatisfiedLinkError" comes out. I have tried to take the measures as Forum has suggested, but it failed.
    The source code is very simple to demonstrate JNI.
    "HelloWorld.h"
    #ifndef INCLUDEDHELLOWORLD_H
    #define INCLUDEDHELLOWORLD_H
    class HelloWorld
    public:
    void hello();
    #endif
    "HelloWorld.cpp"
    #include <iostream>
    #include "HelloWorld.h"
    using namespace std;
    void HelloWorld::hello()
    cout << "Hello, World!" << endl;
    "JHelloWorld.java"
    public class JHelloWorld
    public native void hello();
    static
    System.loadLibrary("hellolib");
    public static void main(String[] argv)
    JHelloWorld hw = new JHelloWorld();
    hw.hello();
    "JHelloWorld.cpp"
    #include <iostream>
    #include <jni.h>
    #include "HelloWorld.h"
    #include "JHelloWorld.h"
    JNIEXPORT void JNICALL Java_JHelloWorld_hello (JNIEnv * env, jobject obj)
    HelloWorld hw;
    hw.hello();
    All the files are in the same directory and all the processes are under the dirctory:
    1. javac JHelloWorld.java
    2. javah -classpath . JHelloWorld
    3. g++ -c -I/usr/java/jdk1.3/include -I/usr/java/jdk1.3/include/linux JHelloWorld.cpp HelloWorld.cpp
    4. ld -shared -o hellolib.so *.o
    5. java -cp . -Djava.library.path=. JHelloWorld
    Exception in thread "main" java.lang.UnsatisfiedLinkError: no hellolib in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1349)
    at java.lang.Runtime.loadLibrary0(Runtime.java:749)
    at java.lang.System.loadLibrary(System.java:820)
    at JHelloWorld.<clinit>(JHelloWorld.java:7)
    Tried another measure:
    i) export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
    ii)java -cp . JHelloWorld
    The same error came out as above.
    I really don't know what is wrong with it.
    Would you like to help me as soon as possible?
    Thanks.
    Regards,
    Johnson

    Hi Fabio,
    Thanks a lot for your help.
    It is very kind of you.
    Regards,
    Johnson

  • A problem about EAX

    A problem about EAXX We have some problem in our development when using "Creative OpenAL SDK . (ver 3.03)". We create "OpenAL device" using "Generic Software"
    and play audio with EAX2.0. But the EAX effect works on one computer while it doesn't work on another one. Followings are the two computers' configuration:
    The computer which works well with EAX:
    Operation system: Windows XP SP3
    Motherboard chipset: Intel i945G
    Sound card: Creative Sound Blaster Audigy Digital Deluxe
    OpenAL.dll version: 6.4.0357.22
    Wrap_oal.dll version: 2..8.
    The computer which doesn't work with EAX:
    Operation system: Windows XP SP3
    Motherboard chipset: Intel i945GC
    Sound card: Creative Sound Blaster Audigy Digital Deluxe
    OpenAL.dll version: 6.4.0357.22
    Wrap_oal.dll version: 2..8.

    Hello gozhu_g,
    I suggest getting in touch with the developers on your inquiry. Here's their site.

  • Problem: program outputs numbers in scientific notation

    my problem is that my program outputs the population in scientific notation instead of round the number to the nearest one. ex: it should say 30787949.57 instead of 3.078794957 E7
    // Calculates the poulation of Mexico City from 1995 to 2018.
    // displays the year and population
    class PopulationCalculator {
    static double r2(double x) {
         //this method rounds a double value to two decimal places.
    double z=((double)(Math.round(x*100)))/100;
    return z;
    } //end method r2
    public static void main(String args[]) {
         double population=15600000.0;
         double rate=0.03;
         System.out.println("Mexico City Population, rate="+r2(rate));
         System.out.println("Year    Population");
         for (int year=1995; year<=2018;year++)  {
             System.out.println(year+ "    "+r2(population));
        population+=rate*population;
        }//end for loop
        System.out.println("The population of Mexico City reaches 30 million on 02/13/17 at 5:38:34am");
        }//end main
        }//end PopulationCalculator
    {code/]

A: problem: program outputs numbers in scientific notation

Or upgrade to JDK 5.0 and user the new java.util.Formatter capability.
You control the rounding and get localization of the fomatted string at
no extra charge. A quick example:
class A {
    public static void main(String[] args) {
        double d = 30787949.57d;
        System.out.println(java.lang.String.format("%,17.2f", d));
}Example output for three different locales:
$ javac -g A.java
$ LC_ALL=fr_FR   java A
    30 787 949,57
$ LC_ALL=en_NZ   java A
    30,787,949.57
$ LC_ALL=it_IT     java A
    30.787.949,57For more information, refer to:
http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html#formatter

Or upgrade to JDK 5.0 and user the new java.util.Formatter capability.
You control the rounding and get localization of the fomatted string at
no extra charge. A quick example:
class A {
    public static void main(String[] args) {
        double d = 30787949.57d;
        System.out.println(java.lang.String.format("%,17.2f", d));
}Example output for three different locales:
$ javac -g A.java
$ LC_ALL=fr_FR   java A
    30 787 949,57
$ LC_ALL=en_NZ   java A
    30,787,949.57
$ LC_ALL=it_IT     java A
    30.787.949,57For more information, refer to:
http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html#formatter

  • BO XI R2 - decimal numbers are truncated

    Hi all,
    When we create query in webitelligence the measure "item price" the decimal numbers are truncated/not shown. The right number is 23.123,55 but we get 23.123,00 ?
    Configuration:
    WIN 2003 server,
    BOE XI r2 => IIS,
    SAP int. kit XI r2
    Thank you for reply!
    Gregor

    Hi Roman,
    you can download the supported platforms list here [http://service.sap.com/bosap-support]. Just navigate to Documentation->Supported Platforms.
    The work-around with the special system user may be used if you have truncation problems on your BOBJ server after installing the integration Kit for SAP. Please keep in mind that this is a work-around when using the integration Kit for SAP and not a recommendation for every installation. As far as I know this problem will be fixed in one of the future fix packs (at least for XI 3.x).
    Regards,
    Stratos

  • Problem about Cups!

    Hey AF.
    I just got a Keycera FS3800 Laser printer - And want to setup this for networking printing!
    I have my server where I have installed Cups and edited along the wikiguide to cups.
    But each time I want to print a testpage - it stops the printer.
    I have installed the following packages:
    cups - ghostscript - foomatic-filters and downloaded the ppd and copied into models. BUT nothing makes me able to print to the printer (Using network) - so that I able to set this printer up for anybody in my house no matter OS ??
    Or know if this is a special driver or anything else ???
    Have anybody any ideas or can help me getting further?

    peque wrote:
    Of course.
    The printer is connected using rj45 and nothing else.
    My network is made with a LAN zone(where the printer is)  with a IPTABLES firewall to the outside.
    I have swith 24ports connected to the firewall and thats where allo PC´s are connected to unless they are using the wireless - buit still they are connected using this switch.
    I have 1 PC (laptop) connected directly to the printer - works just fine.
    But I would´like to use a cups-server, for alle PC in the house. So no matter what they have to go via the cups-server.
    I have copying the ppd file that I got to work on the laptop - and using the exactly same settings about the printer. But when I try to print a test page - THe cups server returns with the printer stopped. After from what I can see - there´s no problem about the network - I can ping, telnet and everything else - But the only problem is that the machine I want to use as cups server will not print with the printer.
    The laptop prints just fine (I used the kde guide to install the printer on this - and another laptop wioth windows also prints fine directly on the port)
    Hoefully this was some more info - Or write me which details you want!
    Sorry, beyond my skill level. I print to a network printer which is attached to a router with a standard ethernet cable. The printer is just a device on the network. The router is the "firewall" to the outside world and the gateway to the Internet for the PCs on the network.
    Good luck, jimbo

  • Maybe you are looking for