1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sbxdbl.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basic.hxx"
33 #include <tools/errcode.hxx>
34 #include <basic/sbx.hxx>
35 #include "sbxconv.hxx"
36 #include "runtime.hxx"
38 double ImpGetDouble( const SbxValues
* p
)
44 SbxBase::SetError( SbxERR_CONVERSION
);
48 nRes
= p
->nChar
; break;
50 nRes
= p
->nByte
; break;
53 nRes
= p
->nInteger
; break;
56 nRes
= p
->nUShort
; break;
58 nRes
= p
->nLong
; break;
60 nRes
= p
->nULong
; break;
62 nRes
= p
->nSingle
; break;
65 nRes
= p
->nDouble
; break;
67 nRes
= ImpCurrencyToDouble( p
->nLong64
); break;
69 nRes
= static_cast< double >(p
->nInt64
); break;
71 nRes
= ImpSalUInt64ToDouble( p
->uInt64
); break;
73 case SbxBYREF
| SbxDECIMAL
:
75 p
->pDecimal
->getDouble( nRes
);
79 case SbxBYREF
| SbxSTRING
:
85 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
86 SbxBase::SetError( SbxERR_CONVERSION
);
92 if( ImpScan( *p
->pString
, d
, t
, NULL
) != SbxERR_OK
)
95 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
96 SbxBase::SetError( SbxERR_CONVERSION
);
104 SbxValue
* pVal
= PTR_CAST(SbxValue
,p
->pObj
);
106 nRes
= pVal
->GetDouble();
109 SbxBase::SetError( SbxERR_NO_OBJECT
); nRes
= 0;
114 case SbxBYREF
| SbxCHAR
:
115 nRes
= *p
->pChar
; break;
116 case SbxBYREF
| SbxBYTE
:
117 nRes
= *p
->pByte
; break;
118 case SbxBYREF
| SbxINTEGER
:
119 case SbxBYREF
| SbxBOOL
:
120 nRes
= *p
->pInteger
; break;
121 case SbxBYREF
| SbxLONG
:
122 nRes
= *p
->pLong
; break;
123 case SbxBYREF
| SbxULONG
:
124 nRes
= *p
->pULong
; break;
125 case SbxBYREF
| SbxERROR
:
126 case SbxBYREF
| SbxUSHORT
:
127 nRes
= *p
->pUShort
; break;
128 case SbxBYREF
| SbxSINGLE
:
129 nRes
= *p
->pSingle
; break;
130 case SbxBYREF
| SbxDATE
:
131 case SbxBYREF
| SbxDOUBLE
:
132 nRes
= *p
->pDouble
; break;
133 case SbxBYREF
| SbxCURRENCY
:
134 nRes
= ImpCurrencyToDouble( *p
->pLong64
); break;
135 case SbxBYREF
| SbxSALINT64
:
136 nRes
= static_cast< double >(*p
->pnInt64
); break;
137 case SbxBYREF
| SbxSALUINT64
:
138 nRes
= ImpSalUInt64ToDouble( *p
->puInt64
); break;
141 SbxBase::SetError( SbxERR_CONVERSION
); nRes
= 0;
146 void ImpPutDouble( SbxValues
* p
, double n
, BOOL bCoreString
)
152 // Hier sind Tests notwendig
154 aTmp
.pChar
= &p
->nChar
; goto direct
;
156 aTmp
.pByte
= &p
->nByte
; goto direct
;
159 aTmp
.pInteger
= &p
->nInteger
; goto direct
;
162 aTmp
.pLong
= &p
->nLong
; goto direct
;
164 aTmp
.pULong
= &p
->nULong
; goto direct
;
167 aTmp
.pUShort
= &p
->nUShort
; goto direct
;
169 aTmp
.pSingle
= &p
->nSingle
; goto direct
;
171 case SbxBYREF
| SbxDECIMAL
:
173 SbxDecimal
* pDec
= ImpCreateDecimal( p
);
174 if( !pDec
->setDouble( n
) )
175 SbxBase::SetError( SbxERR_OVERFLOW
);
179 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
180 p
= &aTmp
; goto start
;
182 // ab hier nicht mehr
184 p
->nInt64
= ImpDoubleToSalInt64( n
); break;
186 p
->uInt64
= ImpDoubleToSalUInt64( n
); break;
189 p
->nDouble
= n
; break;
191 case SbxBYREF
| SbxSTRING
:
195 p
->pString
= new XubString
;
196 ImpCvtNum( (double) n
, 14, *p
->pString
, bCoreString
);
200 SbxValue
* pVal
= PTR_CAST(SbxValue
,p
->pObj
);
202 pVal
->PutDouble( n
);
204 SbxBase::SetError( SbxERR_NO_OBJECT
);
207 case SbxBYREF
| SbxCHAR
:
210 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCHAR
;
212 else if( n
< SbxMINCHAR
)
214 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCHAR
;
216 *p
->pChar
= (xub_Unicode
) n
; break;
217 case SbxBYREF
| SbxBYTE
:
220 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXBYTE
;
224 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
226 *p
->pByte
= (BYTE
) n
; break;
227 case SbxBYREF
| SbxINTEGER
:
228 case SbxBYREF
| SbxBOOL
:
231 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXINT
;
233 else if( n
< SbxMININT
)
235 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMININT
;
237 *p
->pInteger
= (INT16
) n
; break;
238 case SbxBYREF
| SbxERROR
:
239 case SbxBYREF
| SbxUSHORT
:
242 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXUINT
;
246 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
248 *p
->pUShort
= (UINT16
) n
; break;
249 case SbxBYREF
| SbxLONG
:
252 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXLNG
;
254 else if( n
< SbxMINLNG
)
256 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINLNG
;
258 *p
->pLong
= (INT32
) n
; break;
259 case SbxBYREF
| SbxULONG
:
262 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXULNG
;
266 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
268 *p
->pULong
= (UINT32
) n
; break;
269 case SbxBYREF
| SbxSINGLE
:
272 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXSNG
;
274 else if( n
< SbxMINSNG
)
276 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINSNG
;
278 else if( n
> 0 && n
< SbxMAXSNG2
)
280 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXSNG2
;
282 else if( n
< 0 && n
> SbxMINSNG2
)
284 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINSNG2
;
286 *p
->pSingle
= (float) n
; break;
287 case SbxBYREF
| SbxSALINT64
:
288 *p
->pnInt64
= ImpDoubleToSalInt64( n
); break;
289 case SbxBYREF
| SbxSALUINT64
:
290 *p
->puInt64
= ImpDoubleToSalUInt64( n
); break;
291 case SbxBYREF
| SbxDATE
:
292 case SbxBYREF
| SbxDOUBLE
:
293 *p
->pDouble
= (double) n
; break;
294 case SbxBYREF
| SbxCURRENCY
:
297 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCURR
;
299 else if( n
< SbxMINCURR
)
301 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCURR
;
303 *p
->pLong64
= ImpDoubleToCurrency( n
); break;
306 SbxBase::SetError( SbxERR_CONVERSION
);