根据我使用的编译器,我在n = 0时得到一个不同的输出。
std::string ToStrWPrec(double a_value, const int n)
{
std::ostringstream out;
out << std::setprecision(n) << a_value;
return out.str();
}
(GCC)4.8.3 20140911(Red Hat 4.8.3-9)为ToStrWPrec(1.2345678,0)返回1。对于相同的代码,VS2013返回1.2346。
我的问题是:
这是基于以下评论的更新代码
std::string ToStrWPrec(double a_value, const int n)
{
std::ostringstream out;
out << std::setprecision(n) << std::fixed<< a_value;
return out.str();
}
答案 0 :(得分:2)
根据22.4.2.2.2 [facet.num.put.virtuals]第5段,第1阶段,这是关于精度的说法:
对于从浮点类型的转换,如果在转换规范中将
01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: FATAL EXCEPTION: main 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: Process: com.example.ukasz.carrom, PID: 2303 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ukasz.carrom/com.example.ukasz.carrom.ManagePlayers}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:100) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.widget.ListView.setAdapter(ListView.java:491) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at com.example.ukasz.carrom.ManagePlayers.playersListView(ManagePlayers.java:62) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at com.example.ukasz.carrom.ManagePlayers.onCreate(ManagePlayers.java:75) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6237) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 01-06 00:00:44.078 2303-2303/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
指定为精度。否则,不指定精度。
同一段落在其他地方指定定义结果的格式说明符为 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true">
<include layout="@layout/content_manage_players" />
</android.support.design.widget.CoordinatorLayout>
。
floatfield != (ios_base::fixed | ios_base::scientific), str.precision()
的默认值没有根据27.5.5.2 [basic.ios.cons]第3段,表128设置:
%g
floatfield
因此,它归结为格式字符串flags()
如何格式化值。 7.21.6.1第8段中的C标准指出:
如果非零,则 P 等于精度,如果省略精度则等于6,如果精度为零,则为1。
看来,正确的结果是skipws | dec
。