2
$\begingroup$

I have a function giving me this:

(5 Coulomb)/(6 Second)

as output. I want this in decimal form and would like to have the variables taken off the end for unit analysis. How would I do this? I have tried NumberForm, but I think that the unassigned variables I am using for units are throwing it off.

EDIT: Here is what I have for code

In[1]: f[P_, V_] := P/V
In[2]: f[100 W, 120 V]
Out[2]:(5 Coulomb)/(6 Second)

I took your advice and put:

In[1]: f[P_, V_] := P/V
In[2]: f[100 W, 120 V]//N[#]&

And got:

Out[2]:(0.833333 Coulomb)/(Second)
$\endgroup$
5
  • 2
    $\begingroup$ N[(5 coulomb)/(6 second)] gives me (0.833333 coulomb)/second. You could add /. {coulomb -> 1, second -> 1} at the end to get rid of the coulomb and second. $\endgroup$
    – user484
    Jul 6, 2015 at 19:49
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Jul 6, 2015 at 19:49
  • $\begingroup$ When I do Quantity[5, "Coulombs"]/Quantity[6, "Seconds"], then I don't get what you have. Could you make a small example and include code? More importantly, can you say what you mean by variables? I only see numbers and units.. $\endgroup$
    – halirutan
    Jul 6, 2015 at 19:50
  • 1
    $\begingroup$ You have asked the same exact question again only 8 min after this one: How do I extract variables from an expression. You should not spam the site with duplicate questions. $\endgroup$
    – MarcoB
    Jul 6, 2015 at 20:08
  • $\begingroup$ @MarcoB, read what I said in my post. I asked two separate questions, one per post, which is what you are supposed to do on StackExchange sites. One was about converting a number to decimal form and the other was about extracting units from an expression. $\endgroup$ Jul 6, 2015 at 20:13

1 Answer 1

3
$\begingroup$

To be a bit more systematic, I would suggest declaring the units you want to work with in a global variable, and then defining a function unitsToUnity that sets all of those units to unity as follows:

$myUnits = {Coulomb, Second};

unitsToUnity[expression_] := 
 expression /. Thread[$myUnits -> 1]

(5 Coulomb)/(6 Second) // unitsToUnity

(* ==> 5/6 *)
$\endgroup$
1
  • $\begingroup$ Sorry, I guess I was unclear about what I was asking. I got an answer to this in another question; what I was really asking here was how to make sure my output was in decimal format instead of being given as a fraction. Rahul answered that for me in his comment above. $\endgroup$ Jul 7, 2015 at 17:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.