** Make the given Decimal the result. */
| 4685 | ** Make the given Decimal the result. |
| 4686 | */ |
| 4687 | static void decimal_result(sqlite3_context *pCtx, Decimal *p){ |
| 4688 | char *z; |
| 4689 | int i, j; |
| 4690 | int n; |
| 4691 | if( p==0 || p->oom ){ |
| 4692 | sqlite3_result_error_nomem(pCtx); |
| 4693 | return; |
| 4694 | } |
| 4695 | if( p->isNull ){ |
| 4696 | sqlite3_result_null(pCtx); |
| 4697 | return; |
| 4698 | } |
| 4699 | z = sqlite3_malloc( p->nDigit+4 ); |
| 4700 | if( z==0 ){ |
| 4701 | sqlite3_result_error_nomem(pCtx); |
| 4702 | return; |
| 4703 | } |
| 4704 | i = 0; |
| 4705 | if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){ |
| 4706 | p->sign = 0; |
| 4707 | } |
| 4708 | if( p->sign ){ |
| 4709 | z[0] = '-'; |
| 4710 | i = 1; |
| 4711 | } |
| 4712 | n = p->nDigit - p->nFrac; |
| 4713 | if( n<=0 ){ |
| 4714 | z[i++] = '0'; |
| 4715 | } |
| 4716 | j = 0; |
| 4717 | while( n>1 && p->a[j]==0 ){ |
| 4718 | j++; |
| 4719 | n--; |
| 4720 | } |
| 4721 | while( n>0 ){ |
| 4722 | z[i++] = p->a[j] + '0'; |
| 4723 | j++; |
| 4724 | n--; |
| 4725 | } |
| 4726 | if( p->nFrac ){ |
| 4727 | z[i++] = '.'; |
| 4728 | do{ |
| 4729 | z[i++] = p->a[j] + '0'; |
| 4730 | j++; |
| 4731 | }while( j<p->nDigit ); |
| 4732 | } |
| 4733 | z[i] = 0; |
| 4734 | sqlite3_result_text(pCtx, z, i, sqlite3_free); |
| 4735 | } |
| 4736 | |
| 4737 | /* |
| 4738 | ** SQL Function: decimal(X) |
no outgoing calls
no test coverage detected