Message Boards Message Boards

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

[?] Convert units with UnitConvert and CurrencyConvert?

Posted 4 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 4 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 4 years ago

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

POSTED BY: Mac McDowell
Posted 4 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

I don’t know if this is what you’re asking, but if the input code has been written incorrectly, the output (the result) will be the same as the input code. And usually Mathematica will tell you what you did wrong. Example: PersonData[“BrianJacques, NotableBooks” the output would be: PersonData[“BrianJacques, NotableBooks” and then Mathematica would tell you that you didn’t type an ending right square bracket.

Posted 4 years ago

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

POSTED BY: Mac McDowell

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