如何在 PostgreSQL 中将字符串转换为双精度?

yaz*_*com 23 postgresql

如何在 PostgreSQL 中将字符串转换为双精度?

我试过类似的东西:

update points set latitude2 = cast(latitude as double) ;
Run Code Online (Sandbox Code Playgroud)

其中纬度是一个字符串,纬度2是一个双精度值。我只是无法让它工作。

Jac*_*las 46

double 不是 postgres 数据类型:

select cast('324342' as double);
Run Code Online (Sandbox Code Playgroud)
错误:类型“double”不存在
第 1 行:select cast('324342' as double);
                                ^

但是double precision是:

select cast('132342' as double precision);
Run Code Online (Sandbox Code Playgroud)
| 浮动8 |
| :----- |
| 132342 |

所以尝试:

update points set latitude2 = cast(latitude as double precision) ;
Run Code Online (Sandbox Code Playgroud)

如果您愿意,可以使用float*代替,因为根据文档

没有指定精度的浮点数表示双精度

select cast('132342' as float);
Run Code Online (Sandbox Code Playgroud)
| 浮动8 |
| :----- |
| 132342 |

db<>在这里摆弄


* float(n)where 25<=n<=53 因为这也意味着双精度