Postgresql中的十进制值在Node.js中作为字符串返回 [英] Decimal value in Postgresql returned as String in Node.js

查看:46
本文介绍了Postgresql中的十进制值在Node.js中作为字符串返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 node.js 服务器上对 postgresql 数据库运行查询时,我得到的值是变量类型 string ,实际上它是Postgresql数据库中的 十进制 .

When I run a query to my postgresql database on a node.js server, the value that I am getting is of variable type string when in fact it is a decimal in the postgresql database.

我不确定为什么我的小数或什至bigInts都以字符串类型返回.我将knex用作我的ORM(如果有区别).到目前为止,我在网上阅读的内容并不十分清楚该怎么做,而且似乎这种情况会自动发生以保持精度.

I am not sure why my decimals or even bigInts are returning as type strings. I am using knex as my ORM if that makes a difference. So far what I have read online is not very clear about what to do, and it seems as if this happens automatically to preserve precision???

对此最好的解决方法是什么?最好使用parseFloat将查询返回的字符串变量转换为十进制吗?

What is the best work-around for this? Is it best to convert the string variable I am returned from my query into a decimal using parseFloat?

推荐答案

decimalbigint类型都可能包含太大的值,无法适合" JavaScript的Number:

Both decimal and bigint types may contain values that are too large to "fit" in JavaScript's Number:

  • Number.MAX_SAFE_INTEGER (JS): 9007199254740991
  • bigint: -9223372036854775808 to 9223372036854775807
  • decimal: up to 131072 digits before the decimal point; up to 16383 digits after the decimal point

如果您确定数据库中的值适合Number,则可以将其转换(我不知道Knex,但也许它具有某种挂钩系统,可用于转换数据从数据库中检索到的数据),或将数据库架构更改为包含较小"的行类型.

If you're quite certain that the values in your database will fit in Number, you can convert them (I don't know Knex, but perhaps it has some sort of hook system that you can use to transform data that was retrieved from the database), or change your database schema to contain "smaller" row types.

或者,Node也有各种大整数" 程序包也许可以使用.

Alternatively, there are also various "big integer" packages for Node that you might be able to use.

这篇关于Postgresql中的十进制值在Node.js中作为字符串返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆