typescript 如何在打字稿中声明和处理浮点类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50210486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to declare and deal with float type in typescript?
提问by Tamer Hussien
I have angular App which interact with backend required to send json
like this:
我有与后端交互的角度应用程序,需要json
像这样发送:
{"prop": 654646f}
I have two problems, I can't find the type float in typescript
also if the value have quoted in default JSON
it isn't accepted. So how to remove quotes from certain property value in JSON
?
我有两个问题,typescript
如果值默认引用JSON
它不被接受,我也找不到类型 float 。那么如何从 中的某些属性值中删除引号JSON
?
回答by ochs.tobi
There is no float
type in Typescript. All numbers are from the type number
.
float
Typescript 中没有类型。所有数字都来自类型number
。
If you want to cast an string
as number you can simply do it with the +
operator. Example:
如果你想投射一个string
数字,你可以简单地用+
操作符来完成。例子:
myNumberString: string = "25";
myNumber: number = +myNumberString;
回答by Yohan Dahmani
A cleaner look is to do
更干净的外观是要做的
myNumber: number = Number(myNumberString);