问题标签 [type-conversion]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
8 回答
356123 浏览

java - 如何在不强制转换的情况下将 double 转换为 long?

在不强制转换的情况下将 double 转换为 long 的最佳方法是什么?

例如:

0 投票
38 回答
1577075 浏览

python - 如何检查字符串是否为数字(浮点数)?

检查字符串是否可以在 Python 中表示为数字的最佳方法是什么?

我目前拥有的功能是:

这不仅丑陋和缓慢,而且看起来很笨重。但是,我还没有找到更好的方法,因为float()在 main 函数中调用更糟糕。

0 投票
30 回答
4476819 浏览

python - 如何将字符串解析为浮点数或整数?

在 Python 中,如何解析数字字符串,例如

到其对应的浮点值,

? 或者将字符串解析为"31"整数,31

我只想知道如何将float str解析为 a float,以及(单独)将int str解析为 an int

0 投票
3 回答
289 浏览

javascript - JavaScript 如何处理 ++ 运算符?

JavaScript 使用对象进行时髦的自动转换:

将打印:

这是因为 +,如果任何参数是对象/字符串,将尝试将所有参数转换为字符串,然后将它们连接起来。如果所有参数都是数字,则将它们加在一起。* 和 unary + 使用 toString(以及 valueOf,此处未显示)将对象转换为数字。

JavaScript 对 ++ 运算符有什么作用?

0 投票
9 回答
22091 浏览

sql - 将一天的名称转换为其整数表示

在 SQL Server 中,您可以使用 DATENAME 函数以字符串形式获取星期几

返回“星期二”

并且您可以使用 DATEPART 函数以整数形式获取星期几

返回 3

但是假设我有一个包含字符串“Tuesday”的 varchar,我想将其转换为 3 的整数表示。当然,我可以毫不费力地写出转换,但我更愿意使用内置函数. 有这样的功能吗?

0 投票
5 回答
4484 浏览

javascript - Arrays keys number and "number" are unexpectedly considered the same

I have been playing with javascript arrays and I have run into, what I feel, are some inconsistencies, I hope someone can explain them for me.

Lets start with this:

There is nothing special about this code, but I understand that a javascript array is an object, so properities may be add to the array, the way these properities are added to an array seems inconsistent to me.

Before continuing, let me note how string values are to be converted to number values in javascript.

  • Nonempty string -> Numeric value of string or NaN

  • Empty string -> 0

So since a javascript array is an object the following is legal:

p>

The output is unexpected.

The non empty string "4" is converted into its numeric value when setting the property myArray["4"], this seems right. However the empty string "" is not converted into its numeric value, 0, it is treated as an empty string. Also the non empty string "something" is not converted to its numeric value, NaN, it is treated as a string. So which is it? is the statement inside myArray[] in numeric or string context?

Also, why are the two, non numeric, properities of myArray not included in myArray.length and myArray.join(", ")?

0 投票
3 回答
6396 浏览

python - 是否有独立的 Python 类型转换库?

有没有独立的类型转换库?

我有一个只能理解字节/字符串的数据存储系统,但我可以标记元数据,例如要转换为的类型。

我可以破解一些简单的类型转换器系统,就像在我之前所有其他应用程序所做的那样,或者我可以希望使用一个独立的库,除非我找不到。这种常见的活动很奇怪。

只是为了澄清,我会有类似的东西:

('123', 'integer') 我想出去 123

0 投票
15 回答
649346 浏览

javascript - 如何使用 JavaScript 中的格式规范将字符串转换为日期时间?

如何通过指定格式字符串将字符串转换为 javascript 中的日期时间对象?

我正在寻找类似的东西:

0 投票
2 回答
1382 浏览

c# - 请解释这个 Convert.ToInt64 InputStringFormat 异常

谁能告诉我为什么:

失败,而您必须这样做:

因为它看起来很愚蠢!

0 投票
4 回答
29244 浏览

c - 在纯 C 中打印 void 类型

我有一个功能

我想传递一个 void 指针并将其打印到屏幕上。如果 i 是整数、浮点数或双精度,则上面的示例很好,但如果 i 是字符,则崩溃。C 中没有像我通常在 C++ 中使用的重载。所以问题是这样的,我们是否可以在 C 中创建一个函数来打印它的参数元素,如果是的话,这怎么可能,因为此刻我完全无法理解。