将大数归一到小归一化数 [英] Bringing a big number to a small normalized number
问题描述
你好,我有这个代码:
Hello, i have this code:
private void button1_Click(object sender, EventArgs e)
{
double number =Convert.ToDouble( textBox1.Text);
string s = (Math.Round(number, 3)*1000).ToString();
int norm=1;
for(int i=0;i<=s.Length-1;i++)
{
norm*=10;
}
label1.Text =Math.Round((Convert.ToDouble(s)/norm),3).ToString() ;
}
这应该带来一个数字,例如11256,125698到1,125或18668951到1,866(理论上)等等.
理论作品(至少在我看来)并且数量很少,但是当我尝试18668951时,它给了我意想不到的效果(15.356).谁能告诉我我在哪里搞砸了? (加倍的数字不足以使用这么大的数字吗?)
this should bring a number like 11256,125698 to 1,125 or 18668951 to 1,866 (theorethicaly) and so on.
Theorethical works (at least for my mind) and with small numbers also but when i tried on 18668951 it gaved me an unexpected rezult(15.356). Can anyone tell me where i messed up? (double isnt enough to work with such big number?)
推荐答案
否,当我尝试过:
No, when I tried it:
double number = Convert.ToDouble("18668951");
string s = (Math.Round(number, 3) * 1000).ToString();
它给定为"18668951000"
因此,您尝试创建一个溢出的int(32位).
尝试将norm
设置为Int64.
[edit]错字-OriginalGriff [/edit]
It gave s as "18668951000"
So you are trying to create an int (32 bits) which overflows.
Try making norm
an Int64 instead.
[edit]Typo - OriginalGriff[/edit]
怎么了?
double l = Math.Ceiling(Math.Log10(number));
double n = number / Math.Pow(10,l-1);
label1.Text = Math.Round(n,3).ToString();
?
请注意(如果需要),在特殊情况下,您必须处理十的幂(例如10, 100, 1000
).
?
Note (if needed) you have to handle as special cases the powers of ten (e.g. 10, 100, 1000
).
您好,
尝试使用此链接,您可以在其中找到许多解决问题的方法
如何在窗口应用程序中形成文本框值.... [ ^ ]
好吧,我建议您简化代码:
将double类型的变量类型用作"Convert.ToDouble(s)"的值,因为它在Math.Round()中太复杂了. 有时,此声明
Hello,
Try this link where you have many solutions for your problem
how to formate textbox value .....in window application[^]
Allso I sugest that you simplify code :
Use variable type of double for value of "Convert.ToDouble(s)" because it is too complex inside Math.Round()
Sometimes this statment
for(int i=0;i<=s.Length-1;i++)
无法正常工作(我仍然找不到原因),所以我将
won''t work correct ( I still didn''t find out why ) so I use
for(int i=0;i<=s.Length-1;i=i+1)
在为变量范数选择int类型时出现错误,它应该为double类型,因为int类型对于在工作期间可能出现的数字范围而言太小程序.
避免在程序中使用的同一命令中进行变量声明,
示例:
And you have error in selecting type of int for variable norm, it should be type of double because int type is too small for a range of numbers that may appear during the work of program.
Avoid variable declaration in the same command that you use in the program,
exmpl :
for(int i=0;i<=s.Length-1;i++)
您所有人的状况都出现了错误
为了停止for..next循环,它应该是i< s.Lenght-1
我已经更改了您的代码,因此现在可以将示例18668951的结果转换为1,867,将11256,125698的结果转换为1,126,结果应该是这样.
看起来像这样:
You allso have error in condition
for stopping the for..next loop, it should be i < s.Lenght-1
I have changed your code so now it gives this result for your example 18668951 to 1,867 or 11256,125698 to 1,126 and it should be like that.
It looks like this :
void Button1Click(object sender, EventArgs e)
{
//
// Declaration of variables
//
string s = "";
double norm = 0;
double number = 0;
double s1 = 0;
int i = 0;
int lenght = 0;
s = textBox1.Text;
number = Convert.ToDouble(s);
s1 = Math.Round(number,3)*1000;
lenght = s1.ToString().Length;
norm=1;
for(i=0;i<lenght-1;i=i+1)
{
norm = norm * 10;
}
label1.Text =Math.Round((s1/norm),3).ToString() ;
}
如果输入的数字根本不是数字,则应该注意输入的数字的相关性.Convert.ToDouble(s)将不起作用.
艾尔最好的
PerićŽeljko
You should allso take care of relevance of entered number , if it is not number at all Convert.ToDouble(s) won''t work.
Al the best
Perić Željko
这篇关于将大数归一到小归一化数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!