如何在 Android 应用程序中禁用科学浮点表示法
我正在执行基本计算器示例。
在除以两个数字时,我得到科学浮动符号的结果。
当我除 3/2
时,我得到结果 1.4E24
,但我需要正常值 1.5
。 我正在使用NDK。 函数使用如下
float res2 = nativeLib.divide(v1, v2);
s= ""+res2;
System.out.println(res2);
result.setText(s); >
函数调用位于 NativeLib 类文件中
公共本机浮点除法(int x,int y);
Function 的实现是 C 语言
JNIEXPORT jfloat JNICALL Java_com_example_Ndkdemo_NativeLib_divide
(JNIEnv * env, jobject obj, jint value1, jint value2)
{ float d= (value1 / value2) ;
return (d);}
I am executing the basic calculator example.
While dividing two numbers I am getting the result in scientific floating notation.
When I divide 3/2
I am getting result 1.4E24
, but I need normal value as 1.5
.
I am using NDK.
The Function use is below
float res2 = nativeLib.divide(v1, v2);
s= ""+res2;
System.out.println(res2);
result.setText(s); >
The Function call is in NativeLib Class File
public native Float divide(int x, int y);
And the Function implementation is is C lang
JNIEXPORT jfloat JNICALL Java_com_example_Ndkdemo_NativeLib_divide
(JNIEnv * env, jobject obj, jint value1, jint value2)
{ float d= (value1 / value2) ;
return (d);}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能希望将 value1/value2 转换为浮点数,而不仅仅是将其分配给浮点数。
You may want to cast the value1/value2 to a float, not just assign it to a float.
您的 C 代码有轻微损坏。您正在使用两个 int 并执行
float = int / int
。这里的问题是转换规则是这样的:C 首先将一个 int 除以另一个,然后转换为 float。因此传递 3, 2 将导致 3/2 == 1,而不是 1.5。你想要的东西更像是
你要回来的Re 1.4e24。这就是返回值被破坏的情况。
这是因为您将除法函数声明为
Float
而不是float
。这些不是同一件事。Float
是一个装箱值,并且绝对与 C 端的jfloat
类型不同。如果您将本机声明更改为
它,那么一切都应该开始正常工作。 (注意“float”上缺少大写字母!)
Your C code is mildly broken. You're taking two ints and doing
float = int / int
. The problem here is that the casting rules are such that C will first divide one int by the other and then cast to float. So passing 3, 2 will result in 3/2 == 1, not 1.5.You want something more like
Re the 1.4e24 you're getting back. That's the return value getting corrupted.
This is because you're declaring the divide function as
Float
instead offloat
. These are NOT the same thing. TheFloat
is a boxed value, and is most definitely not the same type asjfloat
on the C side.If you change the native declaration to
it should all start working correctly. (note the lack of capital letter on 'float'!)