十进制到字符串转换 [英] Decimal to string convertion

查看:92
本文介绍了十进制到字符串转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,

我在显示数字(十进制)值时出现问题。



如果用户输入数值为12.00应该显示为12.00,

如果用户输入数值为12.0它应该显示为12.0,

i已将这些值存储为Float数据类型,我必须执行一些基于的计算这些价值。



i know 12,12.0 12.00相同,但我需要根据用户输入显示。



我的尝试:



i尝试使用

( Convert.ToDecimal( 12  0 ))。ToString(  #。##); 



但它给了我o / p为12

(Convert.ToDecimal( 12  0 ))。ToString(  F); 



o / p为12.00

解决方案

浮点数不存储精度或尾随零:它只是一个数字,与整数相同不存储前导零:您可以输入1234,它与00000000001234的数字相同

如果要以与用户输入的精度相同的精度显示数据,则需要存储有关该数据的信息。精确的数据 - 我建议创建一个名为PrecisionFloat的新类,它同时包含浮点值和小数位数,并使用该信息覆盖ToString。

但是......什么如果用户输入两个值123.00和123.0000,然后将它们一起添加,会发生什么?精度会怎样?结果应该是两位小数还是四位?如果你将它们相乘怎么办? 2,4,6或8?

我怀疑你太努力了:使用与手头任务相关的精度,而不是用户输入!


你不应该使用float,你应该使用 decimal(C#Reference) [ ^ ]存储您的值。


如果没有额外的编程,你就无法做到。



您可以尝试使用0.0#,这将显示一个十进制最小值(即12.0,但也将显示12.0为12.00但它会正确地显示12.1和12.11为12.1和12.11 ...它只是为了你可能遇到麻烦的尾随零。



你能做的是解析初始输入并与数字一起保存格式为#0.0#格式,其中小数点后面的零数是初始解析用户输入的结果

Hello All,
I have a Problem in displaying numeric(decimal) Values.

if user enters numeric Value as 12.00 it should display as 12.00,
if user enters numeric Value as 12.0 it should display as 12.0,
i have stored these value as Float datatype,i have to perform some calculation based on these values.

i know 12,12.0 12.00 are same, but i need to display according to User Input.

What I have tried:

i tried using

(Convert.ToDecimal(12.0)).ToString("#.##");


but it gives me o/p as 12

(Convert.ToDecimal(12.0)).ToString("F");


o/p as 12.00

解决方案

Floats do not store a precision, or trailing zeros: it's just a number, in the same way that an integer doesn't store leading zeros: you can enter 1234 and it's the same number as 00000000001234
If you want to display data in the same precision that the user entered it, you would need to store information about the precision with the data - I'd suggest creating a new class called PrecisionFloat that holds both the floating point value and the number of decimal places, and uses that info to override ToString.
But...what happens if the user enters two values 123.00 and 123.0000, then you add them together? What happens to the precision? Should the result be two decimal or four? What if you multiply them? 2, 4, 6, or 8?
I suspect that you are trying too hard: use precision that is relevant to the task in hand, rather than the user input!


You should not be using float, you should be using decimal (C# Reference)[^] to store your values.


You cannot do it without some extra programming.

You could try using 0.0#, that will show one decimal minimum (i.e 12.0, but will show 12.0 for 12.00 too. But it will show correctly 12.1 and 12.11 as 12.1 and 12.11...it is just for trailing zeroes that you may have trouble with.

What you could do is parse the initial input and along with the number save the format in the form #0.0# where number of zeroes behind the decimal point is the result of your initial parsing of the user input


这篇关于十进制到字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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