double price = 2.07 现在要得到他末尾能被5除的最大和最小的数,比如定义 double Highprice
double Lowprice 则Highprice= 2.10,Lowprice = 2.05如果
double price = 13.80则Highprice= 13.75,Lowprice = 13.85现求根据变量price来算出Highprice和Lowprice的最简单的方法
double Lowprice 则Highprice= 2.10,Lowprice = 2.05如果
double price = 13.80则Highprice= 13.75,Lowprice = 13.85现求根据变量price来算出Highprice和Lowprice的最简单的方法
for( i= 末尾数字-4; i<8;i++)
{
if imod5 =0; break
}
low= i high = i+0.05
double Lowprice 则Highprice= 2.10,Lowprice = 2.05
难道2.95不是最大的啊,不知道是你抄错了还是我看不明白
double price = 13.80 则Highprice= 13.75,//你上面都可以2.07变成2.10了,那这里你怎么最大的是13.75
Lowprice = 13.85 //最小的是13.85
乱七八糟的
我很快给你答案
你先说一下
你的price是几位长啊
固定码
double 型被5整除怎么说啊……
Highprice =(a+1)*5/100
Lowprice =(a-1)*5/100
double Highprice = 0.0;
double Lowprice = 0.0; double tempdoub = 0.0;
string tempstr = ""; tempstr = price.ToString("f1");//四舍五入
if (price == Convert.ToDouble(tempstr))
{
Highprice = price + 0.05;
Lowprice = price - 0.05; }
else
{
if (price < Convert.ToDouble(tempstr))
{
Highprice = Convert.ToDouble(tempstr);
Lowprice = Convert.ToDouble(tempstr) - 0.5;
}
else
{
Highprice = Convert.ToDouble(tempstr)+0.05;
Lowprice = Convert.ToDouble(tempstr);
}
}
{ double highprice = 0;
double lowprice = 0;
double price = 2.07;
Count(price, ref highprice, ref lowprice);
System.Console.WriteLine("price :{0}, highprice{1},lowprice{2} ", price, highprice, lowprice);
Console.ReadKey(); }
public static void Count(double price, ref double highprice, ref double lowprice)
{
int tempprice = (int)(price * 100);
int result = (tempprice / 5);
highprice = ((double)result + 1) * 5 / 100;
lowprice = ((double)result) * 5 / 100;
}
我想问LZ
例如price=15.05
用我的算法
算出来
Highprice = 15.1
Lowprice = 15.05
可不可以
double Highprice = 0.0;
double Lowprice = 0.0; double tempdoub = 0.0;
string tempstr = ""; tempstr = price.ToString("f1");//四舍五入
if (price == Convert.ToDouble(tempstr))
{
Highprice = price + 0.05;
Lowprice = price - 0.05; }
else
{
if (price < Convert.ToDouble(tempstr))
{
Highprice = Convert.ToDouble(tempstr);
Lowprice = Convert.ToDouble(tempstr) - 0.05;
Lowprice = Convert.ToDouble(Lowprice.ToString("f2"));
if (Lowprice == price)
{
Lowprice = Lowprice - 0.05;
}
}
else
{
Highprice = Convert.ToDouble(tempstr)+0.05;
Lowprice = Convert.ToDouble(tempstr);
}
} }
问题解决
double Lowprice = ((double)((int)(price * 100) / 5) * 5) / 100;类似这样几行代码就能得出答案的那种
double max = Math.Round(t, 1) > t? Math.Round(t, 1): Math.Round(t, 1) + 0.1;
double min = max - 0.5;
double min = Math.Round(max - 0.05, 2);
int result = (tempprice / 5);
int result=(tempprice % 5); 余数
highprice = ((double)result + 1) * 5 / 100;
if(result>0)
lowprice = ((double)result) * 5 / 100;
else
lowprice = ((double)result-1) * 5 / 100;
double Lowprice = ((double)((int)(price * 100 - 1) / 5) * 5) / 100;
double min = Math.Round(max - t, 2) == 0.05? Math.Round(max - 0.10, 2): Math.Round(max - 0.05, 2);
double dLowPrice = ((double)((int)(dNowPrice * 100 - 1) / 5) * 5) / 100;dHighPrice = 9.70!!!错!!!应该为9.75!
int len= pstr.Length - pstr.IndexOf('.');
double Highprice ;
double Lowprice;
Highprice =((double)((int)(price * Math.Pow(10,len)) / 5 + 1) * 5) / Math.Pow(10,len);
Lowprice=
((double)((int)(price * Math.Pow(10, len)) / 5) * 5) / Math.Pow(10, len);
Highprice = 13.85
Lowprice = 13.80!!!汗死,这个
int result = (tempprice / 5);
int result2=(tempprice % 5); //余数
highprice = ((double)result + 1) * 5 / 100;
if(result2>0)
lowprice = ((double)result) * 5 / 100;
else
lowprice = ((double)result-1) * 5 / 100;
我的代码肯定可以的.~
double price = 9.70; int tempprice = (int)(price * 100);
int result = (tempprice / 5);
int result2 = (tempprice % 5); //余数
double highprice;
double lowprice;
highprice = ((double)result + 1) * 5 / 100;
if (result2 > 0)
lowprice = ((double)result) * 5 / 100;
else
lowprice = ((double)result - 1) * 5 / 100;highprice = 9.70!!!错!!!
double Highprice = ((double)(((int)(price * 1000) + 1) / 50 + 1) * 50) / 1000;
double Lowprice = ((double)((int)(price * 100 - 1) / 5) * 5) / 100;
前提是LZ要能保证小数是只精确到第二位的
double price = 13.80的时候 Highprice= 13.85,Lowprice = 13.75 但是double price = 9.70的时候 Highprice= 9.7,Lowprice = 9.65很奇怪
不奇怪,你试下这个就明白了 Console.WriteLine(9.70 * 100);
Console.WriteLine((int)(9.70 * 100));
int tempprice = int.Parse((price * 100).ToString());
int result = (tempprice / 5);
int result2 = (tempprice % 5);
highprice = ((double)result + 1) * 5 / 100;
if (result2 > 0)
{
lowprice = ((double)result) * 5 / 100;
}
else
{
lowprice = ((double)result - 1) * 5 / 100;
}
好吧 我被你打败了, 在(int)(price * 100); 转换的时候丢失了精度.
换成int.Parse((price * 100).ToString());就行了. 或者你用其他方式确保不丢失精度也行~
非bug,lz的问题实际上跟数学关系不大,是计算机里浮点数表示的问题。
计算机是二进制的,不能准确的表示十进制里的大部分浮点数,只是非常接近我们看到的那个值,有可能小于,也有可能大于。
而在浮点转整型的过程中,C#是直接取整的,并不是四舍五入,所以会出现(int)(9.70*100)的结果是969的情况,因为9.70实际上是9.699999…,乘100后是969.9999…。