@@ -703,8 +703,8 @@ In the output, the column names are printed in lowercase::
703
703
}
704
704
]
705
705
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.
708
708
709
709
An example of using fetch type handlers for date and number localizations
710
710
is shown in :ref: `thindate ` and :ref: `thinnumber `.
@@ -802,34 +802,73 @@ Fetching Numbers
802
802
By default all numeric columns are mapped to JavaScript numbers. Node.js
803
803
uses double floating point numbers as its native number type.
804
804
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:
808
830
809
831
.. code-block :: javascript
810
832
811
833
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.
813
837
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.
816
842
817
843
.. code-block :: javascript
818
844
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
+ );
820
861
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 );
823
863
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.
827
869
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.
833
872
834
873
.. _datehandling :
835
874
0 commit comments