@@ -703,8 +703,8 @@ In the output, the column names are printed in lowercase::
703703 }
704704 ]
705705
706- See `lowercasecolumn .js <https://github.com/oracle/node-oracledb/
707- tree/main/examples/lowercasecolumn .js> `__ for a runnable example.
706+ See `lowercasecolumns .js <https://github.com/oracle/node-oracledb/
707+ tree/main/examples/lowercasecolumns .js> `__ for a runnable example.
708708
709709An example of using fetch type handlers for date and number localizations
710710is shown in :ref: `thindate ` and :ref: `thinnumber `.
@@ -802,34 +802,73 @@ Fetching Numbers
802802By default all numeric columns are mapped to JavaScript numbers. Node.js
803803uses double floating point numbers as its native number type.
804804
805- When numbers are fetched from the database, conversion to JavaScript’s
806- less precise binary number format can result in “unexpected”
807- representations. For example:
805+ Node.js can also only represent numbers up to 2 ^ 53 which is
806+ 9007199254740992. Numbers larger than this will be truncated.
807+
808+ The primary recommendation for number handling is to use Oracle SQL or
809+ PL/SQL for mathematical operations, particularly for currency
810+ calculations.
811+
812+ When working with numbers in Node.js, the output may result in "unexpected"
813+ representations. For example, a binary floating-point arithmetic purely in
814+ Node.js:
815+
816+ .. code-block :: javascript
817+
818+ console .log (0.2 + 0.7 ); // gives 0.8999999999999999
819+
820+ To reliably work with numbers in Node.js, you can use
821+ :attr: `~oracledb.fetchAsString ` or a
822+ :ref: `fetch type handler <fetchtypehandler >` (see
823+ :ref: `fetchasstringhandling `) to fetch numbers in string format, and then use
824+ one of the available third-party JavaScript number libraries that handles
825+ large values and more precision.
826+
827+ When decimal numbers are fetched from the database, the conversion to
828+ JavaScript's less precise binary number format differs in node-oracledb Thin
829+ and Thick modes. For example:
808830
809831.. code-block :: javascript
810832
811833 const result = await connection .execute (` SELECT 38.73 FROM dual` );
812- console .log (result .rows [0 ]); // gives 38.730000000000004
834+ console .log (result .rows [0 ]);
835+
836+ This query prints ``38.73 `` in node-oracledb Thin mode.
813837
814- Similar issues can occur with binary floating-point arithmetic purely in
815- Node.js, for example:
838+ In node-oracledb Thick mode, this query results in “unexpected”
839+ representations and prints ``38.730000000000004 ``. To alter this default
840+ conversion from decimal to binary number format in Thick mode, you can use a
841+ fetch type handler as shown in the example below.
816842
817843.. code-block :: javascript
818844
819- console .log (0.2 + 0.7 ); // gives 0.8999999999999999
845+ const result = await connection .execute (
846+ ' SELECT 38.73 FROM dual' ,
847+ [],
848+ {
849+ fetchTypeHandler : function (metaData ) {
850+ if (metaData .dbType == oracledb .DB_TYPE_NUMBER ) {
851+ const converter = (v ) => {
852+ if (v !== null )
853+ v = parseFloat (v);
854+ return v;
855+ };
856+ return {type: oracledb .DB_TYPE_VARCHAR , converter: converter};
857+ }
858+ }
859+ }
860+ );
820861
821- Node.js can also only represent numbers up to 2 ^ 53 which is
822- 9007199254740992. Numbers larger than this will be truncated.
862+ console .log (result .rows );
823863
824- The primary recommendation for number handling is to use Oracle SQL or
825- PL/SQL for mathematical operations, particularly for currency
826- calculations.
864+ The output is ``38.73 ``.
865+
866+ This shows that the number was first converted to a string by the database, as
867+ requested in the fetch type handler. The converter function then converted the
868+ string to a floating point number.
827869
828- To reliably work with numbers in Node.js, use ``fetchAsString `` or
829- ``fetchTypeHandler `` (see :ref: `fetchasstringhandling `) to fetch numbers
830- in string format, and then use one of the available third-party
831- JavaScript number libraries that handles large values and more
832- precision.
870+ See `examples/typehandlernum.js <https://github.com/oracle/node-oracledb/tree/
871+ main/examples/typehandlernum.js> `__ for a runnable example.
833872
834873.. _datehandling :
835874
0 commit comments