472,811 Members | 1,233 Online

# convert double to string in fraction format( 0.75 to 3/4) and vise versa

hi all.
In my program I need to convert a double number to stringrepresentation in fraction format and vise versa. For example,convert 0.75 to string "3/4" and convert string "1/2" to 0.5.The class will only need to handle numbers that increment by1/32. That is 1/32, 2/32, 3/32.... 31/32, 1. If the doublenumber is 0.28 which is between 1/4 and 9/32, the string shouldbe 9/32(go with the bigger number).

TIA

--------------------------------
From: paul gao

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>ReuzASqUkECHu6connty0g==</Id>
Nov 16 '05 #1
2 11424
Paul,

quite simple math --

1. multiple the double by the base - 32 in your case, to get the numerator.

2. set the denominator to 32.

3. use Euclid's algorithm to find GCD.

4. divide numerator and denominator by GCD...

See Knuth, Vol1, page 2 for details of Euclid's algorithm - it is a trivial
iterative search method
here is quick hack in case you don't have Knuth handy...

regards
roy fine
namespace GCDExample{
class Class1{
static int GCD(int numer, int denom){
while(true){
int rem = denom%numer;
if(rem == 0) return numer;
denom = numer;
numer = rem;
}
}
static void RunTest(double dbVal){
int numerator = (int)(dbVal * 32);
int denominator = 32;
int gcd = GCD(numerator,denominator);
numerator /= gcd;
denominator /= gcd;
Console.WriteLine("double val:{0}\n numerator: {1}\n denominator:
{2}\n\n",dbVal,numerator,denominator);
}
static void Main(string[] args){
RunTest(0.0625);
RunTest(0.09375);
RunTest(0.25);
RunTest(0.50);
RunTest(0.75);
RunTest(0.9375);
RunTest(0.96873);
RunTest(0.96874);
RunTest(0.96875);
RunTest(0.96876);
RunTest(0.96877);
RunTest(0.96878);
RunTest(0.96879);
RunTest(0.96880);
}
}
}
"paul gao via .NET 247" <an*******@dotnet247.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
hi all.
In my program I need to convert a double number to string representation in
fraction format and vise versa. For example, convert 0.75 to string "3/4"
and convert string "1/2" to 0.5. The class will only need to handle numbers
that increment by 1/32. That is 1/32, 2/32, 3/32.... 31/32, 1. If the double
number is 0.28 which is between 1/4 and 9/32, the string should be 9/32(go
with the bigger number).

TIA

--------------------------------
From: paul gao

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>ReuzASqUkECHu6connty0g==</Id>
Nov 16 '05 #2
Paul

note - the algorithm works for positive integers - you must specifically
handle 0 and negative values.

rlf
"Roy Fine" <rl****@twt.obfuscate.net> wrote in message
news:em**************@tk2msftngp13.phx.gbl...
Paul,

quite simple math --

1. multiple the double by the base - 32 in your case, to get the numerator.
2. set the denominator to 32.

3. use Euclid's algorithm to find GCD.

4. divide numerator and denominator by GCD...

See Knuth, Vol1, page 2 for details of Euclid's algorithm - it is a trivial iterative search method
here is quick hack in case you don't have Knuth handy...

regards
roy fine
namespace GCDExample{
class Class1{
static int GCD(int numer, int denom){
while(true){
int rem = denom%numer;
if(rem == 0) return numer;
denom = numer;
numer = rem;
}
}
static void RunTest(double dbVal){
int numerator = (int)(dbVal * 32);
int denominator = 32;
int gcd = GCD(numerator,denominator);
numerator /= gcd;
denominator /= gcd;
Console.WriteLine("double val:{0}\n numerator: {1}\n denominator:
{2}\n\n",dbVal,numerator,denominator);
}
static void Main(string[] args){
RunTest(0.0625);
RunTest(0.09375);
RunTest(0.25);
RunTest(0.50);
RunTest(0.75);
RunTest(0.9375);
RunTest(0.96873);
RunTest(0.96874);
RunTest(0.96875);
RunTest(0.96876);
RunTest(0.96877);
RunTest(0.96878);
RunTest(0.96879);
RunTest(0.96880);
}
}
}
"paul gao via .NET 247" <an*******@dotnet247.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
hi all.
In my program I need to convert a double number to string representation in fraction format and vise versa. For example, convert 0.75 to string "3/4"
and convert string "1/2" to 0.5. The class will only need to handle numbers that increment by 1/32. That is 1/32, 2/32, 3/32.... 31/32, 1. If the double number is 0.28 which is between 1/4 and 9/32, the string should be 9/32(go
with the bigger number).

TIA

--------------------------------
From: paul gao

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>ReuzASqUkECHu6connty0g==</Id>

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 3 by: jeff_zhang446 | last post by: Hi, I try to convert double to string as below: std::string cnvrtToString(double lValue) { std::ostringstream lStream; lStream << lValue; return lStream.str(); 2 by: Laurence | last post by: I want to convert a double value to string. 1.2345678911 -> "1.234567891" 123 -> "123" I used the following code to convert it: string str = doubleValue.ToString("f9"); but the second... 5 by: Jeff | last post by: I am trying to crete a method that will convert an improper fraction to a mixed number... I am not sure how about how to acomplish this. I know I can get the remainder with the modulus operator... 15 by: Yifan | last post by: Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan 8 by: shiniskumar | last post by: Ive got a double variable dTotal =5.037717235E7 i formatted it using deciFormat.format(dTotal) and got the String value of dTotal=50377172.35 now i have to pass this value as an argument to a... 0 by: aparna12 | last post by: Hi all Please tell me how to Convert double to string in fraction format (.75 to 3/4) in asp.net. Thanks Aparna 21 by: Aman JIANG | last post by: hi I need to do this (convert double to string) fast, safe and portable. Is there any way to do this ? Except the ways following: 1. C++ I/O stream, stringstream (and boost::lexical_cast) 2.... 2 by: yogi_bear_79 | last post by: I have a double of unknown length that I need to split at the decimal. I thought I would convert it either to a string or a char. char seems to be the best since it easily lends itself to... 1 by: Bjorn Brox | last post by: Hi! In germany, norway and France(?) we are using ',' as decimal separator and it always messes up when you convert a double to and from a string where the interface expects double values stored... 0 by: linyimin | last post by: Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for... 0 by: erikbower65 | last post by: Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on... 0 by: kcodez | last post by: As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central... 0 by: Taofi | last post by: I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ... 0 by: Rina0 | last post by: I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in... 5 by: DJRhino | last post by: Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or... 0 by: lllomh | last post by: How does React native implement an English player? 2 by: DJRhino | last post by: Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...