From: aan Date: Tue, 17 Feb 2009 12:51:14 +0000 (+0000) Subject: improve DoubleToQSting X-Git-Tag: V2_0_0~7 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=f2b2666ee77fce0cdb7a59e84013423bc1584cc6;p=modules%2Fgui.git improve DoubleToQSting --- diff --git a/src/TableViewer/TableViewer_Tool.cxx b/src/TableViewer/TableViewer_Tool.cxx index b39554d68..54de5342a 100755 --- a/src/TableViewer/TableViewer_Tool.cxx +++ b/src/TableViewer/TableViewer_Tool.cxx @@ -71,12 +71,69 @@ double TableViewer_Tool::ToDouble( const QString& txt, bool& isDone ) // Function : DoubleToQString // Purpose : convert string to double //================================================================ -QString TableViewer_Tool::DoubleToQString( const double val, const bool replaceDot ) -{ - QString txt = QString::number( val ); - if ( replaceDot ) - txt.replace( '.', ',' ); - return txt; +QString TableViewer_Tool::DoubleToQString( const double theVal, const int thePrecision, const bool IsRemoveInsignificantZeroes, const bool replaceDot ) +{ + char* aResult = new char[256]; + + int aWholePart = int( theVal ); + QString aWholePartStr = QString::number( aWholePart ); + + int i = aWholePartStr.at( 0 ) == '0' ? 1:0; + int aDigitsBeforeDot = aWholePartStr.size() - i; + + int aFractionalPartLen = thePrecision-aDigitsBeforeDot; + int aFieldWidth = aFractionalPartLen > 0 ? aFractionalPartLen:0; + +// char* aFieldWidthStr; +// std::sprintf( aFieldWidthStr, "%d", (int)aFieldWidth ); + + std::string aFieldWidthStr = QString::number( aFieldWidth ).toStdString(); + + std::string aPercent = "%"; + std::string aFormat = ""; + std::string aOption = ""; + + int aNum = (int)( theVal*( 10^thePrecision ) ); + + if ( aNum>0 ) { + aOption = ".*f"; + } + else { + aOption = ".*g"; + } + + aFormat = aPercent+aFieldWidthStr; + aFormat += aOption; + + std::sprintf( aResult, aFormat.c_str(), aFieldWidth, (double)theVal ); + + if ( IsRemoveInsignificantZeroes ) { + string aResultStr = aResult; + string aNewResult = ""; + int counter = 0; + for ( int jj = aResultStr.size()-1; jj>=aWholePartStr.size(); --jj ) { + if ( aResultStr.at(jj) == '0' || aResultStr.at( jj ) == '.' ) { + counter++; + } + else { + break; + } + } + for ( int ii = 0; ii