1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
22 #include <comphelper/errcode.hxx>
23 #include "sbxconv.hxx"
24 #include <runtime.hxx>
26 double ImpGetDouble( const SbxValues
* p
)
32 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
37 nRes
= p
->nChar
; break;
39 nRes
= p
->nByte
; break;
42 nRes
= p
->nInteger
; break;
45 nRes
= p
->nUShort
; break;
47 nRes
= p
->nLong
; break;
49 nRes
= p
->nULong
; break;
51 nRes
= p
->nSingle
; break;
54 nRes
= p
->nDouble
; break;
56 nRes
= ImpCurrencyToDouble( p
->nInt64
); break;
58 nRes
= static_cast< double >(p
->nInt64
); break;
60 nRes
= ImpSalUInt64ToDouble( p
->uInt64
); break;
62 case SbxBYREF
| SbxDECIMAL
:
63 if (!p
->pDecimal
|| !p
->pDecimal
->getDouble(nRes
))
66 case SbxBYREF
| SbxSTRING
:
72 #if HAVE_FEATURE_SCRIPTING
73 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
74 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
81 if( ImpScan( *p
->pOUString
, d
, t
, nullptr ) != ERRCODE_NONE
)
84 #if HAVE_FEATURE_SCRIPTING
85 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
86 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
95 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
97 nRes
= pVal
->GetDouble();
100 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
); nRes
= 0;
105 case SbxBYREF
| SbxCHAR
:
106 nRes
= *p
->pChar
; break;
107 case SbxBYREF
| SbxBYTE
:
108 nRes
= *p
->pByte
; break;
109 case SbxBYREF
| SbxINTEGER
:
110 case SbxBYREF
| SbxBOOL
:
111 nRes
= *p
->pInteger
; break;
112 case SbxBYREF
| SbxLONG
:
113 nRes
= *p
->pLong
; break;
114 case SbxBYREF
| SbxULONG
:
115 nRes
= *p
->pULong
; break;
116 case SbxBYREF
| SbxERROR
:
117 case SbxBYREF
| SbxUSHORT
:
118 nRes
= *p
->pUShort
; break;
119 case SbxBYREF
| SbxSINGLE
:
120 nRes
= *p
->pSingle
; break;
121 case SbxBYREF
| SbxDATE
:
122 case SbxBYREF
| SbxDOUBLE
:
123 nRes
= *p
->pDouble
; break;
124 case SbxBYREF
| SbxCURRENCY
:
125 nRes
= ImpCurrencyToDouble( *p
->pnInt64
); break;
126 case SbxBYREF
| SbxSALINT64
:
127 nRes
= static_cast< double >(*p
->pnInt64
); break;
128 case SbxBYREF
| SbxSALUINT64
:
129 nRes
= ImpSalUInt64ToDouble( *p
->puInt64
); break;
132 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
); nRes
= 0;
137 void ImpPutDouble( SbxValues
* p
, double n
, bool bCoreString
)
143 // Here are tests necessary
145 aTmp
.pChar
= &p
->nChar
; goto direct
;
147 aTmp
.pByte
= &p
->nByte
; goto direct
;
150 aTmp
.pInteger
= &p
->nInteger
; goto direct
;
152 aTmp
.pLong
= &p
->nLong
; goto direct
;
154 aTmp
.pULong
= &p
->nULong
; goto direct
;
157 aTmp
.pUShort
= &p
->nUShort
; goto direct
;
159 aTmp
.pSingle
= &p
->nSingle
; goto direct
;
161 case SbxBYREF
| SbxDECIMAL
:
163 SbxDecimal
* pDec
= ImpCreateDecimal( p
);
164 if( !pDec
->setDouble( n
) )
165 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
169 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
170 p
= &aTmp
; goto start
;
175 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXCURR
;
177 else if( n
< SbxMINCURR
)
179 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINCURR
;
181 p
->nInt64
= ImpDoubleToCurrency( n
);
184 // from here on no longer
186 p
->nInt64
= ImpDoubleToSalInt64( n
); break;
188 p
->uInt64
= ImpDoubleToSalUInt64( n
); break;
191 p
->nDouble
= n
; break;
193 case SbxBYREF
| SbxSTRING
:
197 p
->pOUString
= new OUString
;
198 // tdf#107953 - show 17 significant digits
199 ImpCvtNum( n
, 17, *p
->pOUString
, bCoreString
);
203 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
205 pVal
->PutDouble( n
);
207 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
);
210 case SbxBYREF
| SbxCHAR
:
211 *p
->pChar
= ImpDoubleToChar(n
); break;
212 case SbxBYREF
| SbxBYTE
:
213 *p
->pByte
= ImpDoubleToByte(n
); break;
214 case SbxBYREF
| SbxINTEGER
:
215 case SbxBYREF
| SbxBOOL
:
216 *p
->pInteger
= ImpDoubleToInteger(n
); break;
217 case SbxBYREF
| SbxERROR
:
218 case SbxBYREF
| SbxUSHORT
:
219 *p
->pUShort
= ImpDoubleToUShort(n
); break;
220 case SbxBYREF
| SbxLONG
:
221 *p
->pLong
= ImpDoubleToLong(n
); break;
222 case SbxBYREF
| SbxULONG
:
223 *p
->pULong
= ImpDoubleToULong(n
); break;
224 case SbxBYREF
| SbxSINGLE
:
227 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXSNG
;
229 else if( n
< SbxMINSNG
)
231 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINSNG
;
233 else if( n
> 0 && n
< SbxMAXSNG2
)
235 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXSNG2
;
237 else if( n
< 0 && n
> SbxMINSNG2
)
239 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINSNG2
;
241 *p
->pSingle
= static_cast<float>(n
); break;
242 case SbxBYREF
| SbxSALINT64
:
243 *p
->pnInt64
= ImpDoubleToSalInt64( n
); break;
244 case SbxBYREF
| SbxSALUINT64
:
245 *p
->puInt64
= ImpDoubleToSalUInt64( n
); break;
246 case SbxBYREF
| SbxDATE
:
247 case SbxBYREF
| SbxDOUBLE
:
248 *p
->pDouble
= n
; break;
249 case SbxBYREF
| SbxCURRENCY
:
252 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXCURR
;
254 else if( n
< SbxMINCURR
)
256 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINCURR
;
258 *p
->pnInt64
= ImpDoubleToCurrency( n
); break;
261 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */