Don't print unnecessary digits after decimal point for imperial units.

1 nm = 0.00003937 mils = 0.00000003937 inches,
So there's no reason to print more decimal places than 5 for mils and 8 for inches.

Related: https://gitlab.com/kicad/code/kicad/-/issues/15539


(cherry picked from commit 93581607a8)
This commit is contained in:
Alex Shvartzkop 2024-03-23 21:19:01 +03:00 committed by dsa-t
parent cc3e57b52f
commit c0dba1e00d
1 changed files with 48 additions and 15 deletions

View File

@ -1,7 +1,7 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 1992-2024 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -28,6 +28,22 @@
#include <charconv>
#include <wx/translation.h>
static void removeTralingZeros( wxString& aText )
{
int len = aText.length();
int removeLast = 0;
while( --len > 0 && aText[len] == '0' )
removeLast++;
if( len >= 0 && ( aText[len] == '.' || aText[len] == ',' ) )
removeLast++;
aText = aText.RemoveLast( removeLast );
}
bool EDA_UNIT_UTILS::IsImperialUnit( EDA_UNITS aUnit )
{
switch( aUnit )
@ -305,29 +321,46 @@ wxString EDA_UNIT_UTILS::UI::StringFromValue( const EDA_IU_SCALE& aIuScale, EDA_
break;
}
char buf[50];
const wxChar* format = nullptr;
if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
switch( aUnits )
{
int len = snprintf( buf, sizeof( buf ) - 1, "%.10f", value_to_print );
while( --len > 0 && buf[len] == '0' )
buf[len] = '\0';
case EDA_UNITS::MILS:
#if defined( EESCHEMA )
format = wxT( "%.3f" );
#else
format = wxT( "%.5f" );
#endif
break;
if( len >= 0 && ( buf[len] == '.' || buf[len] == ',' ) )
buf[len] = '\0';
}
else
{
snprintf( buf, sizeof( buf ) - 1, "%.10g", value_to_print );
case EDA_UNITS::INCHES:
#if defined( EESCHEMA )
format = wxT( "%.6f" );
#else
format = wxT( "%.8f" );
#endif
break;
default:
format = wxT( "%.10f" );
break;
}
wxString stringValue( buf, wxConvUTF8 );
wxString text;
text.Printf( format, value_to_print );
removeTralingZeros( text );
if( value_to_print != 0.0 && ( text == wxS( "0" ) || text == wxS( "-0" ) ) )
{
text.Printf( wxS( "%.10f" ), value_to_print );
removeTralingZeros( text );
}
if( aAddUnitsText )
stringValue += EDA_UNIT_UTILS::GetText( aUnits, aType );
text << EDA_UNIT_UTILS::GetText( aUnits, aType );
return stringValue;
return text;
}