作为C#.Net编程的新手,我一直在尝试进行一些算术计算,并且当我想要得到零小数的整数时,我一直坚持操作双打,如下面的代码所示
double a, b;
a = 2.0;
b = 2.0;
double z = a + b;
Console.WriteLine(z);
输出:4,而不是4.0 请问任何决议?
答案 0 :(得分:1)
如果double不是整数(即小数点右边有数字),我假设您不希望舍入输出值,在这种情况下,为所有值格式化为“N1” z
的内容是不合适的。
if ((z % 1) == 0)
Console.WriteLine(z.ToString("N1")); // should output "4.0"
else
Console.WriteLine(z); // should output "4.123456789"
答案 1 :(得分:0)
使用字符串格式执行此操作:
android.support.test.espresso.PerformException: Error performing 'scroll RecyclerView to: holder with view: has descendant: with id: com.refactor:id/header' on view 'with id: com.refactor:id/playing_recycler'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:80)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at com.ui.home.nowplaying.BaseNowPlayingFragmentEspressoTest.checkNowPlayingMoreLikeThisDoesExist(BaseNowPlayingFragmentEspressoTest.java:197)
at com.ui.home.playing.LikeThisTest.testThisIsDisplayed(LikeThisTest.java:37)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at com.test.base.DelayedFinishActivityTestRule$DelayedFinishActivityTestStatement.evaluate(DelayedFinishActivityTestRule.java:226)
at android.support.test.rule.ServiceTestRule$ServiceStatement.evaluate(ServiceTestRule.java:329)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1996)
Caused by: java.lang.RuntimeException: Found more than one sub-view matching holder with view: has descendant: with id: com.refactor:id/header
*** Matched ViewHolder item at position: 2 ***
View Hierarchy:
+>PercentRelativeLayout{id=-1, visibility=VISIBLE, width=0, height=0, has-focus=false, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=true, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+->TypeFaceTextView{id=2131690060, res-name=more_like_this_header, visibility=VISIBLE, width=0, height=0, has-focus=false, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=true, is-selected=false, root-is-layout-requested=true, has-input-connection=false, x=0.0, y=0.0, text=More like this, input-type=0, ime-target=false, has-links=false}
|
*** Matched ViewHolder item at position: 7 ***
View Hierarchy:
+>PercentRelativeLayout{id=-1, visibility=VISIBLE, width=0, height=0, has-focus=false, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=true, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+->TypeFaceTextView{id=2131690060, res-name=more_like_this_header, visibility=VISIBLE, width=0, height=0, has-focus=false, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=true, is-selected=false, root-is-layout-requested=true, has-input-connection=false, x=0.0, y=0.0, text=More like this, input-type=0, ime-target=false, has-links=false}
|
at android.support.test.espresso.contrib.RecyclerViewActions$ScrollToViewAction.perform(RecyclerViewActions.java:361)
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
N是格式类型(数字),1表示1位小数。
答案 2 :(得分:0)
以下是另一种控制小数格式的方法:
var a = 2.0;
var b = 2.0;
double z = a + b;
Console.WriteLine(string.Format("{0:0.00}", z));
Console.WriteLine(String.Format("{0:0.0000}", z));
上面的输出:
4.00
4.0000
还有一个例子:
var a = 2.0;
var b = 2.2;
double z = a + b;
Console.WriteLine(string.Format("{0:0.0}", z));
Console.WriteLine(String.Format("{0:0.000}", z));
上面的输出:
4.2
4.200
答案 3 :(得分:-3)
没有问题。数字为4.您写入控制台的字符串是使用隐式格式生成的。要更改此设置,只需定义所需的格式,例如:
Console.WriteLine(@"This is my number {z:N1}");
或
Console.WriteLine(z.ToString("N1");
或
{{1}}
MSDN中的implicit conversion部分解释了字符串的格式化类型如何在.NT中工作。像Formatting Types in the .NET Framework和Console.WriteLine这样的方法允许您指定一个预定义或自定义格式字符串,用于控制值如何转换为字符串