I'm trying to write a code to change all data brought in with decimal places to fractions e.g. 20.375 to 20 3/8. I'm using C# I tried this code below but it's not taking it. I'm new to C# so I'm using ChatGPT to assist me but it's giving me code that isn't being accepted by the designer application.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using Stimulsoft.Controls;
using Stimulsoft.Base.Drawing;
using Stimulsoft.Report;
using Stimulsoft.Report.Dialogs;
using Stimulsoft.Report.Components;
using System.Reflection; // Needed for GetMethod
namespace Reports
{
public class Cut_List_250120_105534 : Stimulsoft.Report.StiReport
{
public Cut_List_250120_105534()
{
this.InitializeComponent();
// Registering the method GetFractionFromDecimal for use in report expressions
this.Dictionary.Variables.Add(new Stimulsoft.Report.Dictionary.StiVariable(
"GetFractionFromDecimal",
typeof(string),
typeof(Cut_List_250120_105534).GetMethod("GetFractionFromDecimal", new[] { typeof(double) })));
}
#region StiReport Designer generated code - do not modify
// Report components go here
#endregion StiReport Designer generated code - do not modify
// Method to calculate GCD (Greatest Common Divisor)
private int GCD(int a, int b)
{
while (b > 0) // Using > instead of != for comparison
{
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// Method to convert decimal to a simplified fraction
public string DecimalToFraction(double value)
{
// Tolerance to determine the precision of the fraction
double tolerance = 0.0000001;
int denominator = 1;
// Find the nearest fraction by increasing the denominator until the value is accurate
while (Math.Abs(value - Math.Round(value * denominator) / denominator) > tolerance)
{
denominator = denominator * 10; // Replacing shorthand *= with full multiplication
}
// Calculate the numerator
int numerator = (int)(value * denominator);
// Simplify the fraction by finding the GCD and dividing both numerator and denominator by it
int gcd = GCD(numerator, denominator);
numerator = numerator / gcd; // Replacing shorthand /= with full division
denominator = denominator / gcd;
// If the fraction has no fractional part, return only the whole part
int wholePart = numerator / denominator;
int fractionalNumerator = numerator % denominator;
// Use == to check if the fractional part is 0 (no remainder)
if (fractionalNumerator == 0)
return wholePart.ToString();
else
return wholePart + " " + fractionalNumerator + "/" + denominator;
}
// This method will be called from expressions in the report to convert decimals to fractions
public string GetFractionFromDecimal(double decimalValue)
{
return DecimalToFraction(decimalValue);
}
}
}
Change decimals to fractions
-
- Posts: 1
- Joined: Mon Jan 20, 2025 8:33 pm
Change decimals to fractions
- Attachments
-
- Screenshot 2025-01-20 153548.jpg (278.82 KiB) Viewed 454 times
-
- Posts: 7018
- Joined: Tue Mar 20, 2018 5:34 am
Re: Change decimals to fractions
Hello,
What product and version are you use? Any code in the report will work only in the Compilation mode (CalculationMode property).
Thank you.
What product and version are you use? Any code in the report will work only in the Compilation mode (CalculationMode property).
Thank you.