File indexing completed on 2024-05-12 05:55:17

0001 // This file is part of the SpeedCrunch project
0002 // Copyright (C) 2013 @heldercorreia
0003 // Copyright (C) 2015 Pol Welter <polwelter@gmail.com>
0004 //
0005 // This program is free software; you can redistribute it and/or
0006 // modify it under the terms of the GNU General Public License
0007 // as published by the Free Software Foundation; either version 2
0008 // of the License, or (at your option) any later version.
0009 //
0010 // This program is distributed in the hope that it will be useful,
0011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 // GNU General Public License for more details.
0014 //
0015 // You should have received a copy of the GNU General Public License
0016 // along with this program; see the file COPYING.  If not, write to
0017 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018 // Boston, MA 02110-1301, USA.
0019 
0020 #include "numberformatter.h"
0021 
0022 #include "settings.h"
0023 #include "quantity.h"
0024 
0025 QString NumberFormatter::format(Quantity q)
0026 {
0027     Settings* settings = Settings::instance();
0028 
0029     Quantity::Format format = q.format();
0030     if (format.base == Quantity::Format::Base::Null) {
0031         switch (settings->resultFormat) {
0032         case 'b':
0033             format.base = Quantity::Format::Base::Binary;
0034             break;
0035         case 'o':
0036             format.base = Quantity::Format::Base::Octal;
0037             break;
0038         case 'h':
0039             format.base = Quantity::Format::Base::Hexadecimal;
0040             break;
0041         case 'n':
0042             format.base = Quantity::Format::Base::Decimal;
0043             break;
0044         case 'f':
0045             format.base = Quantity::Format::Base::Decimal;
0046             break;
0047         case 'e':
0048             format.base = Quantity::Format::Base::Decimal;
0049             break;
0050         case 'g':
0051         default:
0052             format.base = Quantity::Format::Base::Decimal;
0053             break;
0054         }
0055     }
0056 
0057     if (format.mode == Quantity::Format::Mode::Null) {
0058         if (format.base == Quantity::Format::Base::Decimal) {
0059           switch (settings->resultFormat) {
0060           case 'n':
0061               format.mode = Quantity::Format::Mode::Engineering;
0062               break;
0063           case 'f':
0064               format.mode = Quantity::Format::Mode::Fixed;
0065               break;
0066           case 'e':
0067               format.mode = Quantity::Format::Mode::Scientific;
0068               break;
0069           case 'g':
0070           default:
0071               format.mode = Quantity::Format::Mode::General;
0072               break;
0073           }
0074         } else {
0075             format.mode = Quantity::Format::Mode::Fixed;
0076         }
0077     }
0078 
0079     if (format.precision == Quantity::Format::PrecisionNull)
0080         format.precision = settings->resultPrecision;
0081     if (format.notation == Quantity::Format::Notation::Null) {
0082         if (settings->resultFormatComplex == 'c')
0083             format.notation = Quantity::Format::Notation::Cartesian;
0084         else if (settings->resultFormatComplex == 'p')
0085             format.notation = Quantity::Format::Notation::Polar;
0086     }
0087 
0088     return DMath::format(q, format);
0089 }