- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何检查napi_number类型的给定napi_value是整数还是小数(带小数的数字) value)通过使用node.js native N-API函数。看起来 N-API 中没有 isInt() 或 isDouble() 等效函数(我们也不想使用 V8 函数调用)。让我们考虑这样一个场景:我们通过传递 JavaScript 对象作为参数来从 JavaScript 调用 native 插件函数 f1(),如代码片段所示。
let obj = { n1: 123, n2: 123.45 };
myaddon.f1( obj );
native 函数 f1() 希望通过调用最佳拟合值提取 N- 来提取与键 n1 和 n2 关联的值API函数。例如,要提取 n1 的值,最好使用 napi_get_value_int* 之一,类似地,对于 n2 的 double
napi_get_value_double
napi_get_value_int32
napi_get_value_uint32
napi_get_value_int64
不幸的是,我找不到任何 N-API 函数来验证 napi_number 属性的导数。您遇到过类似的情况吗?如果遇到过,您是如何解决的?
最佳答案
已向 node-addon-api 团队提出了有关此功能的请求,他们提供了听起来合乎逻辑的答案。我想与这个社区分享答案,这可能有助于其他人可能有的类似疑问。 Here is the answer from node-addon-api team
While handling numbers with JavaScript, it is important to know that all numbers in JavaScript are double-precision 64 bit IEEE 754 values (despite some engines like v8 might have additional represents on small integers, there is no such definition in ECMA spec and no way to determine these types of number in JavaScript).
napi_get_value_{double,int32,uint32,int64} just convert these value to its desired one. There might be a precision loss in the conversion. If a determined number is required in the case, use BigInt instead.
关于c - 如何使用node.js N-API函数检查napi_number类型的napi_value是整数还是小数,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58586425/
我是一名优秀的程序员,十分优秀!