c# 算法

dongyangmoney 2010-11-08 04:19:18
现在有两个数例如:
double a = 1.1;
double b = 100.11;
要求实现 根据a的小数位数去拆分b
返回两个结果
一个为跟 a 有相同小数位数的数,一个为原数减去前面获得的那个有相同小数位数的数,
如上例中应该返回 100.1 和 0.01

(注意a 或b 无小数的情况)
...全文
248 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
peng2739956 2010-11-09
  • 打赏
  • 举报
回复
T-bag 难道你还没被抓?????????
哥子谭 2010-11-08
  • 打赏
  • 举报
回复
提醒注意一下数据精度的问题
@井九 2010-11-08
  • 打赏
  • 举报
回复

static int GetPrecise(double b)
{
if (Math.Abs(b - (int)b) < 1e-10)
return -1;
int precise = 0;
double temp = Math.Abs(b - (int)b);
while((temp = temp*10) < 1 )
{
++precise;
}
return precise;
}
static void Main(string[] args)
{
double a = 1.1;
double b = 100.11;
int precisea = GetPrecise(a);
double result1 = ((int)(b*Math.Pow(10,precisea+1)))/(double)Math.Pow(10,precisea+1);
double result2 = b - result1;
Console.WriteLine(result1);
Console.WriteLine((float)result2);
}
dongyangmoney 2010-11-08
  • 打赏
  • 举报
回复
我自己写出来了但是感觉这个 if else 太多了 不效率
dongyangmoney 2010-11-08
  • 打赏
  • 举报
回复
 double a = 1.11;
double b = 100.11;

double qian = 0.0;
double hou = 0.0;

string str = string.Empty;


int length = 0;
int aindex = a.ToString().IndexOf('.');
if (aindex == -1)
{
length = -1;
}
else
{
length = a.ToString().Length - aindex - 1;
}


int bindex = b.ToString().IndexOf('.');

if (bindex == -1)
{
qian = double.Parse(b.ToString());
}
else
{
if (bindex + length + 1 > b.ToString().Length)
{
qian = double.Parse(b.ToString());
}
else
{
qian = double.Parse(b.ToString().Substring(0, bindex + length + 1));
str = "0.0";
for (int i = 1; i < b.ToString().Length - b.ToString().IndexOf('.') - 1; i++)
{
str += "0";
}
hou = double.Parse((b - qian).ToString(str));

}
}
caoshuming_500 2010-11-08
  • 打赏
  • 举报
回复
字符串处理,一直比较头痛。学习一下
  • 打赏
  • 举报
回复
我上面这个函数,各种情况都测过了
  • 打赏
  • 举报
回复
public void Compute()
{
double dblData1 = 1.1;
double dblData2 = 100.11;
double dblData3 = 0;
double dblData4 = 0;
string strValue = "";
int intLength = 0;

int intIndex = 0;
int intIndex1 = 0;

intIndex = dblData1.ToString().IndexOf('.');
if (intIndex == -1)
intLength = 0;
else
{
intIndex++;

if (intIndex >= 0)
intLength = dblData1.ToString().Length - intIndex;
else
intLength = 0;
}

intIndex1 = dblData2.ToString().IndexOf('.');
intIndex1++;
if (dblData2.ToString().Length >= intIndex1 + intLength)
if (intIndex1 == 0 && intLength == 0)
strValue = dblData2.ToString();
else
strValue = dblData2.ToString().Substring(0, intIndex1 + intLength);
else
strValue = dblData2.ToString();

if (strValue!="")
if (strValue.Substring(strValue.Length - 1, 1) == ".")
strValue = strValue.Substring(0, strValue.Length - 1);

dblData3 = Convert.ToDouble(strValue);
dblData4 = Convert.ToDouble(Convert.ToDecimal(dblData2) - Convert.ToDecimal(dblData3));
}
HolyPlace 2010-11-08
  • 打赏
  • 举报
回复

double a = 1.01;
double b = 100.11;

string strA = a.ToString();
string strB = string.Empty;
string strC = string.Empty;
strB = b.ToString().Substring(0, b.ToString().IndexOf('.') + (strA.Length - strA.IndexOf('.')));

strC = "0.0";
for (int i = 1; i < b.ToString().Length - b.ToString().IndexOf('.') -1; i++)
{
strC += "0";
}
strC = (b - double.Parse(strB)).ToString(strC);




strB 和 strC就是
边城的刀声 2010-11-08
  • 打赏
  • 举报
回复

double a = 1.1;
double b = 100.11;

String strA = a.ToString();
Int32 dotNumLenth = strA.IndexOf('.') == -1 ? 0 : strA.Split('.')[1].Length;

double result1 = Convert.ToDouble(b.ToString(String.Format(@"N{0}", dotNumLenth)));
double result2 = b - result1;

Console.WriteLine(result1);
Console.WriteLine(result2);
龍月 2010-11-08
  • 打赏
  • 举报
回复
情况比较多, 把所有情况列出来 判断吧 此消息通过 【CSDN论坛 Winform测试版】 回复! 龙月.NET的博客
bloodish 2010-11-08
  • 打赏
  • 举报
回复

double a = 1.1;
double b = 100.11;

double ret1 = (long)b + (a-(long)a);
double ret2 = b - ret1;

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧