Math & Science ⇒ Simple calculations package
Simple calculations package
Hello,
Just trying to find out a package that can help me divide a number and potentially round it up/down to an integer, however, all my search results are showing up mathematical formatting topics.
What packages do you know to create division, or at least difference?
Many thanks,
VG
Just trying to find out a package that can help me divide a number and potentially round it up/down to an integer, however, all my search results are showing up mathematical formatting topics.
What packages do you know to create division, or at least difference?
Many thanks,
VG
Last edited by User1802 on Mon Jun 13, 2011 3:06 pm, edited 1 time in total.
NEW: TikZ book now 40% off at Amazon.com for a short time.
-
- Posts: 162
- Joined: Wed Jun 17, 2009 10:18 pm
Re: Simple calculations package
Thank you T,
Just wondering, which macro should I call, can you give me an example? I'm a rookie yet and the macro source doesn't help me much. Couldn't find much documentation on simpler things either.
Many thanks,
VG
Just wondering, which macro should I call, can you give me an example? I'm a rookie yet and the macro source doesn't help me much. Couldn't find much documentation on simpler things either.
Many thanks,
VG
-
- Posts: 162
- Joined: Wed Jun 17, 2009 10:18 pm
Simple calculations package
An example with pgfmath:
This prints "87 divided by 17 is approximately 5."
What happens here is that pgfmathparse evaluates the expression given in the braces, and pgfmathresult prints the result of pgfmathparse. The round function rounds the value in its parenthesis to the nearest whole integer, but the result is still printed with a decimal, e.g. "5.0". That's why I've also used the int function, which strips away all decimals, and leaves just the integer.
Code: Select all
\documentclass{article}
\usepackage{pgf}
\begin{document}
87 divided by 17 is approximately \pgfmathparse{int(round(87/17))}\pgfmathresult.
\end{document}
What happens here is that pgfmathparse evaluates the expression given in the braces, and pgfmathresult prints the result of pgfmathparse. The round function rounds the value in its parenthesis to the nearest whole integer, but the result is still printed with a decimal, e.g. "5.0". That's why I've also used the int function, which strips away all decimals, and leaves just the integer.
Re: Simple calculations package
Thanks T,
Just playing with it now, and I have a latex "variable" defined, like this:
\newcommand{\MYNUMBER}{2000}
how can I introduce it inside the formula, I tried these three and I get an error:
- lorem \pgfmathparse{int(round(\MYNUMBER/12))}\pgfmathresult ipsum
- lorem \pgfmathparse{int(round(\MYNUMBER{}/12))}\pgfmathresult ipsum
- lorem \pgfmathparse{int(round({\MYNUMBER{}}/12))}\pgfmathresult ipsum
Thank you again,
VG
Just playing with it now, and I have a latex "variable" defined, like this:
\newcommand{\MYNUMBER}{2000}
how can I introduce it inside the formula, I tried these three and I get an error:
- lorem \pgfmathparse{int(round(\MYNUMBER/12))}\pgfmathresult ipsum
- lorem \pgfmathparse{int(round(\MYNUMBER{}/12))}\pgfmathresult ipsum
- lorem \pgfmathparse{int(round({\MYNUMBER{}}/12))}\pgfmathresult ipsum
Thank you again,
VG
-
- Posts: 162
- Joined: Wed Jun 17, 2009 10:18 pm
Simple calculations package
The code below works fine here, so you need to give us more information, like what error you get.
It could be that you have an older version of PGF, in which case an update will solve the problem. To find out which version you have, add \listfiles before the \documentclass statement (as below), and compile the document. Somewhere in the log-file you'll find a list of the packages loaded and which version they have.
If you need instructions on how to upgrade packages, please tell us which TeX distribution (TeX Live, MikTeX, MacTeX) you're using.
It could be that you have an older version of PGF, in which case an update will solve the problem. To find out which version you have, add \listfiles before the \documentclass statement (as below), and compile the document. Somewhere in the log-file you'll find a list of the packages loaded and which version they have.
If you need instructions on how to upgrade packages, please tell us which TeX distribution (TeX Live, MikTeX, MacTeX) you're using.
Code: Select all
\listfiles
\documentclass{article}
\usepackage{pgf}
\newcommand{\MYNUMBER}{2000}
\begin{document}
\MYNUMBER\ divided by 17 is approximately \pgfmathparse{int(round(\MYNUMBER/17))}\pgfmathresult.
\end{document}
Re: Simple calculations package
Thank you T, you are right, the problem is because of the way the number was written, it was written "2,000", please notice the comma "thousands" separator.
Your code works perfect, thanks very much for this.
VG
Your code works perfect, thanks very much for this.
VG
Re: Simple calculations package
I am trying the following line of code and I get an error:
\pgfmathparse{int(round({\StrSubstitute{12,000.00}{,}{}}/17))}\pgfmathresult
I'm trying to remove the , character, however I get an error. Trying things separately, works fine:
\StrSubstitute{12,000.00}{,}{}
and
\pgfmathparse{int(round(12000.00/17))}\pgfmathresult
However, when I put things together as seen in the first example, they don't work.
Any advice on this?
VG
\pgfmathparse{int(round({\StrSubstitute{12,000.00}{,}{}}/17))}\pgfmathresult
I'm trying to remove the , character, however I get an error. Trying things separately, works fine:
\StrSubstitute{12,000.00}{,}{}
and
\pgfmathparse{int(round(12000.00/17))}\pgfmathresult
However, when I put things together as seen in the first example, they don't work.
Any advice on this?
VG
Simple calculations package
Why don't you use the fp package?
Code: Select all
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{fp,xstring}
\usepackage[autolanguage]{numprint}
\begin{document}
\def\mynumber{120,000.00}
\def\mydiv{17}
$\mynumber/\mydiv=%
\StrDel\mynumber,[\mynumber]%
\FPeval\myresult{round(\mynumber/\mydiv,0)}%
\numprint\myresult$
\end{document}
-
- Posts: 162
- Joined: Wed Jun 17, 2009 10:18 pm
Simple calculations package
Probably has to do with some inner workings of TeX, but there I cannot help you. An option is to save the commaless number to a macro, and use that in the calculation.
Code: Select all
\documentclass{article}
\usepackage{pgf,xstring}
\begin{document}
\StrSubstitute{12,000.00}{,}{}[\number]
\pgfmathparse{int(round(\number/17))}\pgfmathresult
\end{document}