如何在Delphi中将整数转换为浮点数?
例如:int_h := int_var / 1.5 * int_var;
例如:int_h := int_var / 1.5 * int_var;
i*1.0应该转换为浮点数。任何涉及任何类型的浮点数的计算都会隐式地转换为extendend
,然后在赋值时隐式地转换为所需的结果类型。与C/C ++相比,所有计算都在Extended(80位浮点数,x87浮点单元的内部格式)中进行,并稍后转换回来。
real(i)也可能有效。
整数转浮点数
不需要进行类型转换,只需赋值即可。
Float1 := Integer1;
Integer1 := Trunc(Float1); //truncate
或者
Integer1 := Round(Float1); //Round
根据您所拥有的表达式类型,有3种可能的方法。
var
Float: Double;
Int1: Integer;
Int2: Integer;
begin
Int1 := 925;
Int2 := 21;
Float := Int1; // simple form: assign it
Writeln(Float);
Float := Int2 / Int1; // floating point division: assign it
Writeln(Float);
Float := (1.0 * Int2) * Int1; // integer operator: multiply by 1.0 and add parenthesis
Writeln(Float);
end.
输出:
9.25000000000000E+0002
2.27027027027027E-0002
1.94250000000000E+0004
您可以做以下操作:
myFloat := myInteger;
我有这两个(我有更多,但是举例就足够了)重载函数:
interface
function MyFunc(Value:Integer):Integer;overload;
function MyFunc(Value:Double):Double;overload;
implementation
function MyFunc(Value:Integer):Integer;
begin
MyFunc:=Math.Ceil( {Do some extra complicated Math calcs}
*
MyFunc( {¿How to Type Cast as Double?} Value )
);
end;
function MyFunc(Value:Double):Double;
begin
MyFunc:={Do some Math calcs with Value};
end;
interface
function MyFunc_Integer(Value:Integer):Integer;
function MyFunc_Double(Value:Double):Double;
implementation
function MyFunc_Integer(Value:Integer):Integer;
begin
MyFunc_Integer:=Math.Ceil( {Do some extra complicated Math calcs}
*
MyFunc_Double(Value)
);
end;
function MyFunc_Double(Value:Double):Double;
begin
MyFunc_Double:={Do some Math calcs with Value};
end;
所以我的回答是... 不要放置代码´(Double)SomethingOfTypeInteger´(应该可以工作,但编译器不喜欢),而是放置另一个代码´(0.0+SomethingOfTypeInteger)´,我建议不要使用´(1.0*SomethingOfTypeInteger)´,因为它效率要低得多...
永远不要考虑创建这样的函数(它也比1.0*Value更糟糕):
function ConvetIntegerToDouble(Value:Integer):Double;
begin
ConvetIntegerToDouble:=Value;
end;
interface
function MyFunc(Value:Integer):Integer;overload;
function MyFunc(Value:Real48):Real48;overload;
function MyFunc(Value:Real):Real;overload;
function MyFunc(Value:Double):Double;overload;
procedure CanYouGuess;
implementation
var
MyVarOfTypeDouble:Double;
function MyFunc(Value:Integer):Integer;
begin
MyFunc:=Round(MyFunc(1.0+Value));
end;
function MyFunc(Value:Real48):Real48;
begin
MyFunc:=MyFunc(2.0+Value);
end;
function MyFunc(Value:Real):Real;
begin
MyFunc:=MyFunc(4.0+Value);
end;
function MyFunc(Value:Double):Double;
begin
MyFunc:=(8.0+Value);
end;
procedure CanYouGuess;
var
MyVarOfTypeReal48:Real48;
MyVarOfTypeReal:Real;
begin
MyVarOfTypeDouble:=MyFunc( 0 ); // What value will be on MyVarOfTypeDouble?
MyVarOfTypeReal48:=0;
MyVarOfTypeDouble:=MyFunc(MyVarOfTypeReal48); // What value will be on MyVarOfTypeDouble?
MyVarOfTypeReal:=0;
MyVarOfTypeDouble:=MyFunc(MyVarOfTypeReal); // What value will be on MyVarOfTypeDouble?
MyVarOfTypeDouble:=MyFunc(0.0); // What value will be on MyVarOfTypeDouble?
end;
interface
function MyFunc(Value:Double):Integer;overload;
function MyFunc(Value:Double):Real48;overload;
function MyFunc(Value:Double):Real;overload;
function MyFunc(Value:Double):Double;overload;
是的,编译器有时候很傻... 只有在MyFunc用于赋值的情况下才能编译.... 请看以下代码:
MyVarOfTypeReal48:=MyFunc(1); // It can know return value must be Real48, so call that one... but compiler is not so Intelligent
MyVarOfTypeInteger:=Round(5*MyFunc(1)+MyFunc(2)*1.3); // This is problematic, first one could be understod as Integer version, second one as ... as what? Real, Real48 or Double? not possible to know... that is why compiler does not like such overloaded (with same arguments).