Message Boards Message Boards

1
|
8118 Views
|
6 Replies
|
4 Total Likes
View groups...
Share
Share this post:

[?] Convert units with UnitConvert and CurrencyConvert?

Posted 5 years ago

I am a novice user, trying to work through some examples given in book about Mathematica, and I am confused about the output I am receiving. Here is what I am entering, and the output is the same as my input - any idea what I am doing wrong? I have attached the notebook - called "Input equals output"

In[1]:= UnitConvert[26.2 mi, km]

Out[1]= UnitConvert[26.2 mi, km]

In[2]:= CurrencyConvert[$10 .00, Quantity[1, "Euros"]]

During evaluation of In[2]:= Quantity::compat: DimensionlessUnit and Euros are incompatible units

Out[2]= CurrencyConvert[0., Quantity[1, "Euros"]]
Attachments:
POSTED BY: Mac McDowell
6 Replies
Posted 5 years ago

Thanks - there was no error code, but the solution was provided above. I appreciate your help!

POSTED BY: Mac McDowell
Posted 5 years ago

Hi Mac,

UnitConvert takes a Quantity as its first argument. Most units need to be specified as strings.

UnitConvert[Quantity[26.2, "Miles"], "Kilometers"]
CurrencyConvert[Quantity[10, "USDollars"], "Euros"]

Explore the documentation.

POSTED BY: Rohit Namjoshi
Posted 5 years ago

Thank you - that makes sense - this book example is incorrect, and your solution works. Many thanks, Mac

POSTED BY: Mac McDowell
Posted 5 years ago

Hi Mac,

To expand a little on Eleazar's answer.

As a newcomer to WL it is important to understand that unlike most programming languages, WL uses term rewriting. When you define a function, you are actually defining a rewriting or replacement rule.

This defines a replacement where f[<something>] is replaced by <something>^2.

Clear[f, a, b]
f[x_] := x^2

f[2]
(* 4 *)
f[a + b]
(* (a + b)^2 *)

A replacement rule may also restrict the patterns to which it applies. This definition will only apply if the argument to f is an Integer. a + b is not an Integer so no replacement is performed (output = input).

Clear[f, a, b]
f[x_Integer] := x^2

f[2]
(* 4 *)

f[a + b]
(* f[a + b] *)

If you are interested in exploring this aspect of the language further, read this post and the many links it references.

POSTED BY: Rohit Namjoshi

Note that Mathematica uses some pretty formatting on expressions as well. For example, Quantity expressions format in a human-readable way:

input and output of Quantity[26.2,

Notice the slightly lighter text colour.

So the book is likely correct as well.

Other examples of formatted content include: Entity expressions (in an orange box), DateObject expressions (grey box with an icon).

POSTED BY: Gerli Jõgeva
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract