Results 1 to 7 of 7

Thread: Adding 1/8

  1. #1
    Join Date
    May 2003
    Posts
    144

    Unanswered: Adding 1/8

    I have a report that shows the total number of serving. I have 10 customers and each of them is served '1/8' pie. How can I get the report to show '1 2/8' pie? Help.

    Thanks

    SHK

  2. #2
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    use mod to pull your fraction into a parsed string. IE CInt([total servings] / 8) & " " & [total servings] mod 8 & "/8 pie"
    Last edited by Teddy; 01-20-05 at 15:01.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  3. #3
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    how about:

    dim myT as integer
    dim myB as integer
    dim myN as single
    dim myS as string
    myT = 10
    myB = 8
    myN = myT / myB
    myS = cstr(int(myN)) & " " & cstr(myN mod 8) & "/" & cstr(myB)

    izy
    Last edited by izyrider; 01-20-05 at 13:53. Reason: sorry Teddy: my slow typing again !
    currently using SS 2008R2

  4. #4
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    hmmmm. can that idea run into issues with float imprecision????

    maybe this is better done with integer math:

    dim myT as integer
    dim myB as integer
    dim myN as integer
    dim myS as string
    myT = 10
    myB = 8
    myN = myT \ myB
    myS = cstr(myN) & " " & cstr(myT - (myN * myB)) & "/" & cstr(myB)

    type carefully! i really do mean \ and not /

    izy
    currently using SS 2008R2

  5. #5
    Join Date
    May 2003
    Posts
    144
    First of all. Thanks for the formula. It works except that I am now facing another problem where some of the customers are served 1/8 pie, some of them are served 1/4 pie, and some of them get the whole pie. Any idea how can I make this work? Help. Please.

    Thanks

    SHK

  6. #6
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    how many 1/8's is a 1/4?

    See where I"m going here?

    You know how to convert the fractions, explain how to the computer.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  7. #7
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Here is a function for SQL Server. You can copy the logic for Access VB if you like:

    Code:
    CREATE function MixedFraction(@Numerator int, @Denominator int)
    returns varchar(20) as
    begin
    --blindman, 1/21/05
    --returns a simplified mixed fraction from the supplied Numerator and Denominator
    
    --test variables
    -- declare	@Numerator int
    -- declare	@Denominator int
    -- set	@Numerator = 27
    -- set	@Denominator = 18
    
    declare	@Counter numeric(16,8)
    set	@Counter = 1
    
    while	(@Counter <= @Numerator and @Counter <= @Denominator)
    	begin	
    	set	@Counter = @Counter + 1
    		while	((@Numerator/@Counter) = cast(@Numerator/@Counter as int)
    			and
    			(@Denominator/@Counter) = cast(@Denominator/@Counter as int))
    			begin
    			set	@Numerator=@Numerator/@Counter
    			set	@Denominator=@Denominator/@Counter
    			end
    	end
    return	isnull((cast(nullif(@Numerator/@Denominator, 0) as varchar(20)) + ' '), '') + cast(@Numerator % @Denominator as varchar(20)) + '/' + cast(@Denominator as varchar(20))
    end
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •