<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: UnitsManager.formatInternalValue not properly adjusting for precision set in User Preference in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10767563#M7489</link>
    <description>&lt;P&gt;I can do the formatting in my own code if it's necessary. I think that maybe the documentation is misleading.&lt;BR /&gt;&lt;A href="https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-6592d7a9-703e-4878-a690-b9359acb5cda" target="_blank" rel="noopener"&gt;https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-6592d7a9-703e-4878-a690-b9359acb5cda&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 19 Nov 2021 01:17:51 GMT</pubDate>
    <dc:creator>mcd8604</dc:creator>
    <dc:date>2021-11-19T01:17:51Z</dc:date>
    <item>
      <title>UnitsManager.formatInternalValue not properly adjusting for precision set in User Preference</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10766479#M7487</link>
      <description>&lt;P&gt;I am trying to display some internal values to the user formatted to their preferences by using&amp;nbsp;UnitsManager.formatInternalValue, however it doesn't seem to be adjusting the precision properly. For example, the user preferences are set as such (with default Unit of mm):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mcd8604_0-1637250158939.png" style="width: 400px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/991208i03EE9734266B9BBE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mcd8604_0-1637250158939.png" alt="mcd8604_0-1637250158939.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;When I run the following snippet:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;adsk.core.Application.get().activeProduct.unitsManager.formatInternalValue(0.123456789, adsk.core.Application.get().activeProduct.unitsManager.defaultLengthUnits) &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I would expect the return string to be:&lt;BR /&gt;1.235 mm&lt;/P&gt;&lt;P&gt;However, the returned value is:&lt;/P&gt;&lt;P&gt;1.2345679 mm&lt;/P&gt;&lt;P&gt;(note that it is actually rounding at the 7th decimal place)&lt;/P&gt;&lt;P&gt;Am I misunderstanding how the formatting system is supposed to work, or is this a bug?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;edit: I am currently using version&amp;nbsp;&lt;SPAN&gt;2.0.11415.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Nov 2021 15:53:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10766479#M7487</guid>
      <dc:creator>mcd8604</dc:creator>
      <dc:date>2021-11-18T15:53:16Z</dc:date>
    </item>
    <item>
      <title>Re: UnitsManager.formatInternalValue not properly adjusting for precision set in User Preference</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10767490#M7488</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/6681450"&gt;@mcd8604&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I thought the preference settings only affect the GUI display, so I'm not sure if I should consider it a bug or not.&lt;/P&gt;
&lt;P&gt;Why don't you try using Round for now?&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface

        value = 0.123456789

        unitMgr: adsk.core.UnitsManager = app.activeProduct.unitsManager
        precision = app.preferences.unitAndValuePreferences.generalPrecision

        res = [
            f'original : {unitMgr.formatInternalValue(value)}',
            f'use generalPrecision : {unitMgr.formatInternalValue(round(value, precision))}',
        ]

        ui.messageBox('\n'.join(res))
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 19 Nov 2021 00:22:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10767490#M7488</guid>
      <dc:creator>kandennti</dc:creator>
      <dc:date>2021-11-19T00:22:36Z</dc:date>
    </item>
    <item>
      <title>Re: UnitsManager.formatInternalValue not properly adjusting for precision set in User Preference</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10767563#M7489</link>
      <description>&lt;P&gt;I can do the formatting in my own code if it's necessary. I think that maybe the documentation is misleading.&lt;BR /&gt;&lt;A href="https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-6592d7a9-703e-4878-a690-b9359acb5cda" target="_blank" rel="noopener"&gt;https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-6592d7a9-703e-4878-a690-b9359acb5cda&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Nov 2021 01:17:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10767563#M7489</guid>
      <dc:creator>mcd8604</dc:creator>
      <dc:date>2021-11-19T01:17:51Z</dc:date>
    </item>
    <item>
      <title>Re: UnitsManager.formatInternalValue not properly adjusting for precision set in User Preference</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10767699#M7490</link>
      <description>&lt;P&gt;We have to make sure to round while in the preferred units otherwise the precision will be off by the difference of internal unit precision (cm) and whatever is in preferences. In order to do this, we have to convert to that unit then do the rounding.. but then we have to convert back to internal units again to use the format method:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;def _formatInternalValue(value, displayUnits=None, showUnits=True):
	valueConverted = unitsManager.convert(value, 'cm', unitsManager.defaultLengthUnits)
	roundedValueConverted = round(valueConverted, precision)
	roundedValueInternal = unitsManager.convert(roundedValueConverted, unitsManager.defaultLengthUnits, 'cm')
	return unitsManager.formatInternalValue(roundedValueInternal, displayUnits or unitsManager.defaultLengthUnits, showUnits)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;edit: Unfortunately, neither of these rounding methods work when converted to inches, for example. This is due to the conversion naturally adding more decimal places in most cases (i.e. because an inch isn't a whole multiple of centimeters).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think this should help a little. Unfortunately, it doesn't support many of the preferences that UnitsManager.formatInternalValue would and I'm left trying to figure out a better way to do this without re-implementing every formatting preference.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;def _formatInternalValue(value, displayUnits=None, showUnits=True):
	valueConverted = unitsManager.convert(value, unitsManager.internalUnits, displayUnits or unitsManager.defaultLengthUnits)
	roundedValueConverted = round(valueConverted, precision)
	formattedValue = f'{roundedValueConverted}
	if showUnits:
		formattedValue += f' {unitsManager.formatUnits(displayUnits or unitsManager.defaultLengthUnits)}'
	return formattedValue&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Nov 2021 04:11:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10767699#M7490</guid>
      <dc:creator>mcd8604</dc:creator>
      <dc:date>2021-11-19T04:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: UnitsManager.formatInternalValue not properly adjusting for precision set in User Preference</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10767743#M7491</link>
      <description>&lt;P&gt;I tried working with&amp;nbsp;standardizeExpression and this seems to be working much better, though it is lacking the ability to format trailing zeros properly. I still have to test all the different preferences.&lt;/P&gt;&lt;LI-CODE lang="general"&gt;def _formatInternalValue(value, displayUnits=None, showUnits=True):
	convertedValue = unitsManager.convert(value, unitsManager.internalUnits, displayUnits or unitsManager.defaultLengthUnits)
	return unitsManager.standardizeExpression(str(round(convertedValue, precision)), displayUnits or self.unitsManager.defaultLengthUnits)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Nov 2021 04:29:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10767743#M7491</guid>
      <dc:creator>mcd8604</dc:creator>
      <dc:date>2021-11-19T04:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: UnitsManager.formatInternalValue not properly adjusting for precision set in User Preference</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10769957#M7492</link>
      <description>&lt;P&gt;This is a bug.&amp;nbsp; It should be using the unit preferences settings when formatting the string.&amp;nbsp; I'll make sure a bug gets logged.&lt;/P&gt;</description>
      <pubDate>Sat, 20 Nov 2021 01:40:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/unitsmanager-formatinternalvalue-not-properly-adjusting-for/m-p/10769957#M7492</guid>
      <dc:creator>BrianEkins</dc:creator>
      <dc:date>2021-11-20T01:40:37Z</dc:date>
    </item>
  </channel>
</rss>

