Solved

One code, two unit of measure

Posted on 2015-02-04
6
100 Views
Last Modified: 2015-02-05
Hi all.
I'm working on a recipes program and I want to give users the option to choose if use Imperial or metric unit of measure.
I would like they can change their choice  at anytime, so I'm wondering if there is a best practice: could it be something like give different languages?  But math operations could give very different result, so it can't be only a translation issue...
Any idea?

Thank you all in advance
Marco
0
Comment
Question by:Marco Gasi
  • 3
  • 2
6 Comments
 
LVL 100

Expert Comment

by:mlmcc
ID: 40590106
What are you trying to change?

Are you simply wanting to allow the user to see the measurements in different systems or do you also want the ingredients to change to another language?

If it just the measurements you could have a dropdown with the choices then have code on selection that calculates from the current values the new values and displays the new values with the correct measure.

You could also have the different measurements stored with the recipe thus saving the calculations.

mlmcc
0
 
LVL 30

Author Comment

by:Marco Gasi
ID: 40590446
Thanks mlmcc for your reply. No I don't have to change to a different language: it was only an example.
My doubt is this: if in one recipe are needed 50 gr. of oil and I want display it with Imperial system I have to convert those 50 grams to 1,8 ounces. I want prepare a conversion table to do automatically this conversion when the user changes the unit of measure and I would like to know if there is some ready unit or what is the  best practice to do it.
0
 
LVL 25

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 40590603
Keep all values in let say metric units. Use global ratio for all unit types (one for converting gr, one for converting liters, ...).
const
  UNITS_METRIC = 0;
  UNITS_IMPERIAL = 1;
...
  UNITS_MAX = UNITS_IMPERIAL;
const
  UNIT_gr = 0;
  UNIT_L = 1;
..  //temperature unit, ...
  UNIT_m = 2; 
  UNIT_MAX = UNIT_m;

const
  UnitNames: array[0..UNITS_MAX, 0.. UNIT_MAX] of String =
    (
      ('gr', 'L', 'm'),
      ('ounce', 'fl oz', 'foot'),
    );
  XConvert: array[0..UNITS_MAX, 0.. UNIT_MAX] of Double = 
    (
      (1.0, 1.0, 1.0), //metric into metric
      (x.x, x.x, x.x) //metric into imperial
    );
...
var
  iCurrentUnitsIndex: Integer = UNITS_METRIC; //defaule

function DoConvert(fVal: Double; iUntidx, iUnit: integer): Double;
begin
  Result := RoundTo(fVal * XConvert[iUntidx, iUnit], -1);
end;

Open in new window


..  when you define all of these constants ... use in calculations (texts)...
var
  sText: String;
...

 sText := 'For this recipe zou need: '+#13#10+FloatToStr(DoConvert(50, iCurrentUnitsIndex, UNIT_gr) + //50 gr into current units
  ' ' +UnitNames[iCurrentUnitsIndex, UNIT_gr];
...

Open in new window


When you change - let say - in combo - in OnChange event - change value of iCurrentUnitsIndex and do refresh page (all texts)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

Author Comment

by:Marco Gasi
ID: 40590626
Hi Sinisa, How are you?
Thank you for your reply... I guesed I have to use constants, but I really needed the suggestion about how to do it: exactly what you gave me! I'll test with a small app soon. If I'll have some doubt I'll post a new question and I'll post a link to here, right?
Cheers and thank you again.
0
 
LVL 30

Author Closing Comment

by:Marco Gasi
ID: 40590627
Thank you all.
0
 
LVL 25

Expert Comment

by:Sinisa Vuk
ID: 40590642
I'm fine, glad to help you. If you get stuck - make a post here.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Delphi XE7 Rest Client 2 174
Print Graphic and Text to Epson TM-T88v 12 113
Working with hours 3 31
Base1 Encode/Decode 3 29
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now