Salome HOME
Update copyright info (2010->2011)
[modules/smesh.git] / src / PluginUtils / GeomSelectionTools.cxx
index 74a5f64c29509f4a0f0b78930d987f47719cbcad..d8f5b43862f46fab7f73778863d477b6a7f6b40f 100644 (file)
@@ -1,4 +1,4 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D
+//  Copyright (C) 2007-2011  CEA/DEN, EDF R&D
 //
 //  This library is free software; you can redistribute it and/or
 //  modify it under the terms of the GNU Lesser General Public
 //
 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 // ---
 // File    : GeomSelectionTools.cxx
 // Authors : Nicolas GEIMER (OCC)
 // ---
-//
-
 
 #include "GeomSelectionTools.h"
 
@@ -298,5 +297,47 @@ GeomAbs_SurfaceType GeomSelectionTools::getFaceInformation(TopoDS_Shape S)
 }
 
 
+//////////////////////////////////////////
+// Utility functions
+//////////////////////////////////////////
+#include <QLocale>
+#include <QRegExp>
+
+QString PluginUtils::PrintDoubleValue( double theValue, int thePrecision )
+{
+  const double prec = 1e-12;
+  
+  if ( qAbs(theValue) < prec )
+    return "0";
+
+  QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( thePrecision ) );
 
+  if ( prec > 0 ) {
+    int p = 0;
+    while ( p < thePrecision ) {
+      QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( p++ ) );
+      double v = aRes.toDouble();
+      double err = qAbs( theValue - v );
+      if ( err > 0 && err <= prec )
+        break;
+    }
+  }
+
+  // remove trailing zeroes
+
+  QRegExp expre( QString( "(%1|%2)[+-]?[0-9]+$" ).arg( QLocale().exponential().toLower(), 
+                               QLocale().exponential().toUpper() ) );
+
+  int idx = aRes.indexOf( expre );
+  QString aResExp = "";
+  if ( idx >= 0 ) {
+    aResExp = aRes.mid( idx );
+    aRes = aRes.left( idx );
+  }
+
+  if ( aRes.contains( QLocale().decimalPoint() ) )
+    aRes.remove( QRegExp( QString( "(\\%1|0)0*$" ).arg( QLocale().decimalPoint() ) ) );
+
+  return aRes == "-0" ? QString( "0" ) : aRes + aResExp;
+}