双数据类型需要5位小数 [英] Need 5 decimal places in double data type

查看:114
本文介绍了双数据类型需要5位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想表现出如此舒缓的感觉

双倍a = 1.16200而不是1.162



我尝试了什么:



双a = 1.16200

但它显示a = 1.162

I want to show soothing like that
double a = 1.16200 instead of 1.162

What I have tried:

double a = 1.16200
but it's showing a = 1.162

推荐答案

Quote:

在双数据类型中需要5位小数

Need 5 decimal places in double data type



看起来像格式化问题,但你没有展示你的代码。

C#格式编号作为字符串示例 - code-sample.net [ ^ ]

标准数字格式字符串| Microsoft Docs [ ^ ]


数字本身没有格式概念:1.6与1.60000相同,123与0000000000123相同。

他们获得格式的唯一时间是将它们转换为字符串以便呈现给用户,可以通过使用string.Format或ToSting显式地显示,也可以使用Write或WriteLine方法隐式生成输出。 br />


VS中的查看号码使用默认格式,显示最后一个非零数字。



如果要显示特定数量的小数位,则需要在显示数据时使用特定的格式字符串:

Numbers in themselves don't have an concept of format: 1.6 is the same as 1.60000 and 123 is the same as 0000000000123.
The only time they get a format is when you convert them to a string for presentation to the user, either explicitly by using string.Format or ToSting, or implicitly by using a Write or WriteLine method to generate output.

And viewing numbers in VS uses a "default format" which shows up to the last non-zero digit.

If you want to show a specific number of decimal places, you need to use specific format strings when you present the data:
double d1 = 1.162;
double d2 = 1.16200000000001;
Console.WriteLine(


{d1},{d2 });
Console.WriteLine(
"{d1}, {d2}"); Console.WriteLine(


这篇关于双数据类型需要5位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆