2010-02-13 143 views
0

我試圖讓漂浮在我的SQLite表小數點露面。我已經將下面的代碼包含在了遊戲中,這很好。以及在關卡結束時發生的情況。以及它如何更新到sqlite表。在比賽中,它可以用作十進制小數,但它會在整個數字中記錄爲一個整數。所以如果最終時間是52.4,它將顯示爲52.0。我將字段類型設置爲FLOAT。請幫忙。sqlite表中的小數點?

以下是如何在遊戲中鞋時,它通過十分之一秒計數。

-(void)updateTimerLabel{ 

if(appDelegate.gameStateRunning == YES){ 
timeSeconds-= 0.1; 
timerLabel.text=[NSString stringWithFormat:@"Time: %.1f", timeSeconds]; 
} 
} 

這是一個級別的代碼到底...

-(void) endLevel{ 
if(appDelegate.easymediumhard ==2){ 

    if(gameVarLevel!=5){ 
     stageCompleted = [[UIAlertView alloc] initWithTitle:@"Sweet!" message:@"Level Completed. Play next level?" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil]; 
    } 

    else if(gameVarLevel == 5){ 
     globalScore = timeSeconds; 
     if(appDelegate.ScoreID==0) 
     { 
      appDelegate.ScoreID=[appDelegate InsertGame]; 
     } 
     else{ 

      [appDelegate updateScores:appDelegate.ScoreID]; 
     } 

     stageCompleted = [[UIAlertView alloc] initWithTitle:@"Sweet!" message:@"All Levels Complete!\n Play again?" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil]; 
    } 
    } 
} 

,這是它如何被更新的表...

-(void)updateScores:(NSInteger)Primary_key 
{ 
    sqlite3_stmt *statement=nil; 
    NSString *sql=nil; 
    @try 
    { 
     statement=nil; 
     sql=nil; 

if(easymediumhard == 2){ 
      sql=[NSString stringWithFormat:@"update tblHard set Score=? where id=%d",Primary_key]; 
     } 
if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL)!=SQLITE_OK) 
     { 
      NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database)); 
     } 

     sqlite3_bind_int(statement, 1, globalScore); 

     int success=sqlite3_step(statement); 

     if (success == SQLITE_ERROR) { 

      NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database)); 
     } 
     sqlite3_finalize(statement); 
     statement=nil; 
     sql=nil; 
    } 
    @catch (NSException *e) 
    { 
     NSLog(@"asd"); 
    } 
} 

回答

0

使用sqlite3_bind_double代替的sqlite3_bind_int。並確保globalScore實際上是一個浮點變量(您發佈的代碼並不清楚它是否是一個)。

+0

floatint點?那只是讓它在.h文件中浮動globalScore? – NextRev 2010-02-14 00:55:22

+0

它絕對有效,但任何想法如何保持它1或2小數點?現在,它的上市了不少...... – NextRev 2010-02-14 03:34:18

+0

許多十進制數(如0.5)不能準確地在浮點格式表示。你不應該關心存儲在數據庫中的小數位數。在顯示數據時進行數字格式設置(正如您在發佈的代碼中所做的那樣)。你可以做類似'圓(X * 10.0)/ 10.0;'到圓x到一個十進制數字,但在你的情況下,它不會真的改變什麼(因爲0.5不能在浮點來表示)。 – 2010-02-14 12:50:47