- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个求解方程组的程序。当它给我结果时,就像:“x1= 1,36842”。我想得到那个“1,36842”的分数,所以我写了这段代码。
procedure TForm1.Button1Click(Sender: TObject);
var numero,s:string;
a,intpart,fracpart,frazfatta:double;
y,i,mcd,x,nume,denomin,R:integer;
begin
a:=StrToFloat(Edit1.Text); //get the value of a
IntPart := Trunc(a); // here I get the numerator and the denominator
FracPart := a-Trunc(a);
Edit2.Text:=FloatToStr(FracPart);
numero:='1';
for i:= 1 to (length(Edit2.Text)-2) do
begin
numero:=numero+'0';
end; //in this loop it creates a string that has many 0 as the length of the denominator
Edit3.text:=FloatToStr(IntPart);
y:=StrToInt(numero);
x:=StrToInt(Edit3.Text);
while y <> 0 do
begin
R:= x mod y;
x:=y;
y:=R;
end;
mcd:=x; //at the end of this loop I have the greatest common divisor
nume:= StrToInt(Edit3.Text) div mcd;
denomin:= StrToInt(numero) div mcd;
Memo1.Lines.Add('fraction: '+IntToStr(nume)+'/'+IntToStr(denomin));
end;
它不能正常工作,因为它给我的分数是错误的。谁能帮帮我?
最佳答案
您的代码无法运行,因为您使用的是二进制 float 。二进制浮点类型不能表示您要表示的十进制数。可表示的二进制 float 的形式为 s2e,其中 s 是有效数,e 是指数。因此,例如,您不能将 0.1 表示为二进制浮点值。
最明显的解决方案是使用整数运算执行计算。根本不要调用 StrToFloat。不要接触浮点运算。自己解析输入字符串。找到小数点。使用后面的位数来计算十进制。去掉任何前导或尾随零。剩下的使用整数运算。
举个例子,假设输入是'2.79'
。通过处理文本,将其转换为分子和分母变量
Numerator := 279;
Denominator := 100;
显然,您必须编写字符串解析例程而不是使用整数文字,但这是例行程序。
最后,求出这两个整数的gcd来完成本题。
最重要的是,要表示十进制数据并对其进行操作,您需要一种十进制算法。这不包括二进制 float 。
关于delphi - 从它的十进制数中得到一个分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15753273/
我正在尝试编写一个程序,该程序接受十六进制、八进制和小数的输入,将它们存储在整数变量中,并将它们连同它们转换为十进制形式一起输出。例如: 用户输入:0x43、0123、65 程序输出: 0x43 he
var re = /^([0-9]*)(\.[0-9]{2})$/ re.test(.22) true re.test(.20) false re.test(10.02) true re.test(1
我有一个类型为BigDecimal的属性“initialPrice”的“Trade”类。该属性可以 根据另一个类别“Symbol”中包含的属性“decimals”,小数位数会有所不同,因此需要使用不同
我是这里编码的新手。 我正在尝试使用编码来显示平均值、最大值和最小值的统计信息。 我遇到了错误,他们说我无法将 decimal[] 转换为 int[]。我在我的代码旁边评论了下面的错误。这是最后几行之
在不久的将来,我一直在研究小数类型以获得一些可能的编程乐趣,并希望将它用作比 Int64 更大的整数。一个关键点是我需要找出我可以安全地存储为小数(不丢失精度)的最大整数;我这样说,因为显然它在那里使
所以我这样做了: for(i=1;i0;i--) { if(MASKBINARY[i-1]==1 && MASKBINARY[i]==0) // check if the next eleme
我正在尝试将数字四舍五入到最接近的指定小数。 我希望它四舍五入到小数点 .11、.22、.33、.44、.55、.66、.77、.88 但不是 .99。相反,0.99 应该是 1。 例如: 1.14
获取 python (python 3) Decimal 的整数部分和小数部分的最有效方法是什么? 这是我现在拥有的: from decimal import * >>> divmod(Decimal
我有一个使用十进制列表样式的 ol: ol li { list-style-type: decimal; } First Second 编号工作正常,但如何删除句点?而不是显示: 1.
将字节数组转换为具有所选基数的字符串的最佳方法是什么? S.O. 上有大量示例。和其他地方转换为十六进制字符串。我在这里主要感兴趣的是将十六进制或十进制字符串转换为其他;也是一种更通用的方式。 这是我
不清楚十进制类型的 sizeof。以字节为单位的大小是否像在 sql server 中一样因精度而异? c# 类型的精度变量是“十进制”吗? 我不想打开不安全的代码来只调用小数类型的 sizeof。你
我必须向十进制值添加尾随零。不仅用于显示(因此 Format 不是一个选项),而且用于实际的底层数据,因为小数精度在我们的应用程序中很重要。 我试过: decimal value = 1M decim
我正在使用以下代码行将 decimal 转换为 string: decimal a = 0; a.ToString(); Resharper 给了我以下警告:“明确指定字符串区域性”。我想这是有道理的
我注意到 .NET 在涉及小数和尾随零时有一些古怪/不直观的行为。 0m == 0.000m //true 0.1m == 0.1000m //true 但是 (0m).ToString() == (
最近写单片机 RTC 日期、时间配置,需要实现十进制、BCD码互换,将示例Demo分享给各位朋友: BCD是指用二进制来表示十进制数的编码,即:用4位二进制来表示一位十进制数,因此4位二进制数
这似乎是重复的,但我找不到合适的答案(问题足够接近但是..)我有一个代表十进制数的字符串,它总是有很多小数位,至少 20,有时最多 2000 (代表特定的验证计算,即像'是数字 135 到 147 素
我想知道我是否会收到这样的问题: "Convert a decimal number to two's complement, then give your answer in Hex". 下面是路径
此存储过程不返回十进制格式 00.00 的薪水 ALTER PROCEDURE taxable_varsalary @emp_code bigint, @co_id bigint AS de
使用 leaflet R 包,有没有办法将 zoom 设置为非整数(即十进制)值? 也就是说,介于这两个缩放级别之间: 例子 随着 zoom 从第 3 级移动到第 4 级,它看起来好像正在四舍五入到最
我如何编写一个函数来将十进制数字符串转换为十进制数并将十进制数转换为字符串? 最佳答案 有非常方便的 clojure 函数可以将任何内容转换为字符串以及将类似数字的内容转换为 BigDecimal:
我是一名优秀的程序员,十分优秀!