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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:23:29  来源:igfitidea点击:

How to declare and deal with float type in typescript?

angulartypescript

提问by Tamer Hussien

I have angular App which interact with backend required to send jsonlike this:

我有与后端交互的角度应用程序,需要json像这样发送:

{"prop": 654646f}

I have two problems, I can't find the type float in typescriptalso if the value have quoted in default JSONit isn't accepted. So how to remove quotes from certain property value in JSON?

我有两个问题,typescript如果值默认引用JSON它不被接受,我也找不到类型 float 。那么如何从 中的某些属性值中删除引号JSON

回答by ochs.tobi

There is no floattype in Typescript. All numbers are from the type number.

floatTypescript 中没有类型。所有数字都来自类型number

If you want to cast an stringas 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);