Test mergefield to see if it is an Integer - differential formatting

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Test mergefield to see if it is an Integer - differential formatting

Post by Charles Kenyon »

Edit: I solved this by using a picture switch.
{ IF { MERGEFIELD OurFileNumber } = "{ Mergefield OurFileNumber \# 0 }" { MergeField OurFileNumber \# 0 } { MergeField OurFileNumber \# 0.0 } }
Works.

See also Paul Edstein's simpler construction:

{={=INT({MERGEFIELD OurFileNumber}) \# 0}+{=MOD({MERGEFIELD OurFileNumber},1) \# "0.0;;0"}}
He provides useful links below, as well.


Hi, I am trying to assist someone with a merge question. He wants a mergefield to show as an integer without any decimal point if it is an integer and to show with a decimal point and one decimal if not an integer. I've been working on the test for an Integer using either the INT or the ROUND functions without success.
00 deleteme 9.png
I expected this to be simple but am stymied.
I know I need to work more on the decimal format, but currently Round to 0 decimals is giving something different from the original as is INT.

Here are the field results:
00 deleteme 7.png
I am going to refer to Paul Edstein's Field Maths Tutorial to try to figure this out.
https://www.msofficeforums.com/word/387 ... orial.html
I am already seeing that I may want to simply use a picture switch to do the comparison.
Here is a field with a picture switch instead of the functions and it seems to be working.
{ IF "{ MERGEFIELD OurFileNumber }" = "{ Mergefield OurFileNumber \# 0 }" "Truetext" "FalseText" }
{ IF { MERGEFIELD OurFileNumber } = "{ Mergefield OurFileNumber \# 0 }" { MergeField OurFileNumber \# 0 } { MergeField OurFileNumber \# 0.0 } }
This works. Thank you again, Paul.

Here is the original question in case someone wants to weigh in directly:
https://answers.microsoft.com/en-us/mso ... e2e9ed2b82
You do not have the required permissions to view the files attached to this post.
Last edited by Charles Kenyon on 08 Apr 2022, 16:28, edited 2 times in total.

User avatar
HansV
Administrator
Posts: 78455
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Test mergefield to see if it is an Integer - differential formatting

Post by HansV »

Glad to see you were able to solve it.
Best wishes,
Hans

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Test mergefield to see if it is an Integer - differential formatting

Post by macropod »

Simpler:
{={=INT({MERGEFIELD OurFileNumber}) \# 0}+{=MOD({MERGEFIELD OurFileNumber},1) \# "0.0;;0"}}
Paul Edstein
[Fmr MS MVP - Word]

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Test mergefield to see if it is an Integer - differential formatting

Post by Charles Kenyon »

Thank you Paul. I see you posted this in the original thread as well and know that the OP appreciates it.

I have never found good documentation on Word's functions. Perhaps I have not looked hard enough.

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Test mergefield to see if it is an Integer - differential formatting

Post by macropod »

The solution I posted is really just a matter of figuring out how to exploit fully documented field switches and a bit of math to achieve the desired outcome.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Test mergefield to see if it is an Integer - differential formatting

Post by Charles Kenyon »

I'm glad it is easy for someone. Again, I have never found good documentation on Word functions. I tried both INT and MOD before going to my more complex solution.

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Test mergefield to see if it is an Integer - differential formatting

Post by macropod »

No one said it's easy...
Paul Edstein
[Fmr MS MVP - Word]

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Test mergefield to see if it is an Integer - differential formatting

Post by Charles Kenyon »

You've said this is well documented. Any chance of a link to the documentation?

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Test mergefield to see if it is an Integer - differential formatting

Post by macropod »

See, for example:
https://support.microsoft.com/en-us/off ... 8c94aa19f4
https://support.microsoft.com/en-us/off ... 36ae43508c
All my solution has done is to use the documented field switches creatively. You'll find more of that in my Microsoft Word Field Maths Tutorial, available at:
https://www.msofficeforums.com/word/387 ... orial.html
or:
http://www.gmayor.com/downloads.htm#Third_party
Paul Edstein
[Fmr MS MVP - Word]

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Test mergefield to see if it is an Integer - differential formatting

Post by Charles Kenyon »

Thank you.
I've used and very much appreciate your tutorials.
I am talking about INT, MOD, ABS, etc.
I see now that you discuss these in your Word Field Maths Tutorial on Page 4. I had forgotten.

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Test mergefield to see if it is an Integer - differential formatting

Post by macropod »

If you go to: https://support.microsoft.com/en-us/off ... b74aabb6b2,
then click on '= (Formula) field', that will open up: https://support.microsoft.com/en-us/off ... fc2b5bc21d
which has all the available Operators, Functions, Picture Switch details, etc.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Test mergefield to see if it is an Integer - differential formatting

Post by Charles Kenyon »

Thank you, again.