From 1c19f9729ace8fc82654aec721150f0d62af0ba4 Mon Sep 17 00:00:00 2001 From: nds Date: Fri, 20 Feb 2009 11:30:21 +0000 Subject: [PATCH] Small correction for improving of the DoubleToQString method. --- src/TableViewer/TableViewer_Tool.cxx | 93 ++++++++++------------ src/TableViewer/TableViewer_Tool.h | 8 +- src/TableViewer/TableViewer_ViewWindow.cxx | 7 +- 3 files changed, 51 insertions(+), 57 deletions(-) diff --git a/src/TableViewer/TableViewer_Tool.cxx b/src/TableViewer/TableViewer_Tool.cxx index 54de5342a..10f94ebf5 100755 --- a/src/TableViewer/TableViewer_Tool.cxx +++ b/src/TableViewer/TableViewer_Tool.cxx @@ -71,69 +71,60 @@ double TableViewer_Tool::ToDouble( const QString& txt, bool& isDone ) // Function : DoubleToQString // Purpose : convert string to double //================================================================ -QString TableViewer_Tool::DoubleToQString( const double theVal, const int thePrecision, const bool IsRemoveInsignificantZeroes, const bool replaceDot ) -{ - char* aResult = new char[256]; +QString TableViewer_Tool::DoubleToQString( const double value, + const int precision, + const bool removeZeroes, + const bool replaceDot ) +{ + QString aResult = ""; - int aWholePart = int( theVal ); + int aWholePart = int( value ); QString aWholePartStr = QString::number( aWholePart ); - int i = aWholePartStr.at( 0 ) == '0' ? 1:0; + int i = aWholePartStr.at( 0 ) == '0' ? 1:0; int aDigitsBeforeDot = aWholePartStr.size() - i; - - int aFractionalPartLen = thePrecision-aDigitsBeforeDot; - int aFieldWidth = aFractionalPartLen > 0 ? aFractionalPartLen:0; + + int aFractionalPartLen = precision-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"; + // the next row checks belonging the formatted result to ".*f" format + // if the result isn't signed('0.000' for '.00001'), it's necessary use ".*g" + int aNum = (int)( value*( 10^precision ) ); + std::string anOption = aNum > 0 ? ".*f" : ".*g"; + + std::string aFormat = "%"+aFieldWidthStr; + aFormat += anOption; + + char* aBuf = new char[256]; + std::sprintf( aBuf, aFormat.c_str(), aFieldWidth, (double)value ); + + if ( removeZeroes ) { + std::string aResultStr = aBuf; + delete aBuf; + std::string aNewResult = ""; + int aCounter = 0; + for ( int jj = aResultStr.size()-1; jj >=aWholePartStr.size(); --jj ) { + if ( aResultStr.at(jj) == '0' || aResultStr.at( jj ) == '.' ) + aCounter++; + else + break; + } + for ( int ii = 0; ii=aWholePartStr.size(); --jj ) { - if ( aResultStr.at(jj) == '0' || aResultStr.at( jj ) == '.' ) { - counter++; - } - else { - break; - } - } - for ( int ii = 0; iiresourceMgr(); if ( resMgr ) { isExpImg = resMgr->booleanValue( "TableViewer", "export_table_images", true ); dotToComma = resMgr->booleanValue( "TableViewer", "replace_dot_by_comma", true ); - number_precision = resMgr->integerValue( "TableViewer", "number_precision", 6 ); + precision = resMgr->integerValue( "TableViewer", "number_precision", 6 ); } for ( int c = 0, nbC = numCols( type ); c < nbC; c++ ) { @@ -556,7 +556,8 @@ void TableViewer_ViewWindow::exportTableData( Handle(HTMLService_HTMLTable)& tab double aVal = TableViewer_Tool::ToDouble( txt, aDbl ); if ( aDbl ) cell->InsertText( TableViewer_Tool::ToExtString( - TableViewer_Tool::DoubleToQString( aVal, number_precision, false, dotToComma ) ) ); + TableViewer_Tool::DoubleToQString( aVal, + precision, false, dotToComma ) ) ); else cell->InsertText( TableViewer_Tool::ToExtString( txt ) ); } -- 2.39.2