2009年11月17日 星期二

C# 的 Decimal 小數點很麻煩...

最近在算成績時發現,float 真的是不能拿來算成績,不只是不適合算錢,連滿分只有100分的成績也不能算,會算出一堆誤差,花了一堆時間查了半天終於發現有個叫「decimal」型態,有效位數非常大,專用在財務方面的資料型態,decimal 捨入計算誤差非常非常少,後來就全面使用 decimal 來算成績,可是又發現計算結果總是有「.00」出現,就算計算結果是整數,還是有那個00...要拿掉非常麻煩...,又搞了半天,終於發現終極解法,就是運算、儲存時全部用 decimal ,但是要輸出到報表就全部轉型成 float、double,雖然還是有點麻煩,不過算是較簡單的解法了。float、double 不適合運算,但是運算結果輸出去到是很適合,因為成績的圍範就滿分100,最多小數到2位,跟本不會超出 float 的圍範...吧....

我的目得只是要計算結果不能有誤差,但是結果的尾數是0的要去掉...就這樣..

2009年11月9日 星期一

Postgresql Changing a Column's Data Type

Changing a Column's Data Type

To convert a column to a different data type, use a command like:

ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);

This will succeed only if each existing entry in the column can be converted to the new type by an implicit cast. If a more complex conversion is needed, you can add a USING clause that specifies how to compute the new values from the old.

PostgreSQL will attempt to convert the column's default value (if any) to the new type, as well as any constraints that involve the column. But these conversions might fail, or might produce surprising results. It's often best to drop any constraints on the column before altering its type, and then add back suitably modified constraints afterwards.

2009年9月16日 星期三

.NET 如何加密一字串,只讓執行加密的電腦才能解密。

就這樣:
ProtectedData 是 .NET 提供的加密類別,透過此類別加密的資料,只要執行加密的電腦才能解密。

下列程式碼所用到的 System.Security 組件要自行加入參考中。

public static string Protected(string password)

{

if (string.IsNullOrEmpty(password)) return string.Empty;

byte[] plain = Encoding.UTF8.GetBytes(password);

string key = "key";

byte[] cipher = ProtectedData.Protect(plain,

Encoding.UTF8.GetBytes(key),

DataProtectionScope.CurrentUser);

return Convert.ToBase64String(cipher);

}

public static string UnProtected(string base64String)

{

if (string.IsNullOrEmpty(base64String)) return string.Empty;

string key = "key";

byte[] cipher = Convert.FromBase64String(base64String);

byte[] plain = ProtectedData.Unprotect(cipher,

Encoding.UTF8.GetBytes(key),

DataProtectionScope.CurrentUser);

return Encoding.UTF8.GetString(plain);

}


2009年9月10日 星期四

在 Windows 7 上安裝 BootCamp 2.1 (x64) 的方法

我的是 MacBook,一般的 Mac 不知道適用嗎...

1.先把語言改成英文的。
2.執行 BootCamp 2.1 升級程式。

無言的第三行...

2009年9月4日 星期五

讀取 Postgresql 資料 Schema 方法

查詢主機上有哪些資料庫:
SELECT datname FROM pg_database

查詢資料庫有哪些資料表:
SELECT * FROM information_schema.tables

查詢資料庫有哪些資料欄位:
SELECT * FROM information_schema.columns

查詢資料庫的大小:
select pg_database_size('ischool_junior_cgjh_hc')

2009年9月3日 星期四

重設 Postgresql 的 Sequence 方法

這個方法是將 Sequence 設成使用此 Sequence 的資料表 id 最大值。

SELECT setval('dal_log_id_seq', max(id)) FROM student;

2009年9月1日 星期二

使用 Sandcastle Help File Builder 製作 VS.NET 的 HELP 文件

http://itgroup.blueshop.com.tw/jeff377/blog?n=convew&i=3777