Question: How to print a variable with units?

I have a variable with the unit Volt.

I'd like to print something like "The voltage is %f" where the floating point is the value of the variable with the unit.

I've tried the options below so far.

How would one accomplish this?

with(Units:-Simple)

NULL

x := 5*Unit('V')

5*Units:-Unit(V)

(1)

printf(x)

Error, (in fprintf) format string expected

 

printf("%f", x)

Error, (in fprintf) number expected for floating point format

 

printf("%d", x)

Error, (in fprintf) integer expected for integer format

 

printf("%a", x)

5*Units:-Unit(V)

 

printf("%A", x)

5*Units:-Unit(V)

 

printf("%q", x)

5*Units:-Unit(V)

 

printf("%v", x)

5*Units:-Unit(V)

 

printf("%P", x)

                               5 Units:-Unit(V)

 

print(x)

5*Units:-Unit(V)

(2)

print(The*voltage*is, x)

The*voltage*is, 5*Units:-Unit(V)

(3)

print(The*voltage*is*x)

5*The*voltage*is*Units:-Unit(V)

(4)

NULL

Download PrintUnits.mw

Please Wait...