我正在尝试将浮点值转换为整数,修改int值,然后重新转换回浮点值。但是,小数的值会丢失,我很确定我在代码中使用了 static_cast<>()函数错误。
我的代码是二进制乘法器,它将二进制值 f 次移到左边。例如,当我做1.2 x 2之类的东西时,我只得到2而不是2.4。
class Program
{
static void Main(string[] args)
{
//try
//{
List<FolkbokforingspostTYPE> deserializedList = new List<FolkbokforingspostTYPE>();
deserializedList = Deserialize<List<FolkbokforingspostTYPE>>();
var PersonalIdentityNumber = deserializedList.Select(item => item.Personpost.PersonId.PersonNr).ToList();
var FirstName = deserializedList.Select(item => item.Personpost.Namn.Fornamn).ToList();
var LastName = deserializedList.Select(item => item.Personpost.Namn.Efternamn).ToList();
var LanKod = deserializedList.Select(item => item.Personpost.Folkbokforing.LanKod).ToList();
var Kommunkod = deserializedList.Select(item => item.Personpost.Folkbokforing.KommunKod).ToList();
var UtdelAdress2 = deserializedList.Select(item => item.Personpost.Adresser.Folkbokforingsadress.Utdelningsadress2).ToList();
var PostNr = deserializedList.Select(item => item?.Personpost?.Adresser?.Folkbokforingsadress?.PostNr).ToList();
//var PostOrt = deserializedList.Select(item => item.Personpost.Adresser.Folkbokforingsadress.Postort).ToList();
//var FodelselanKod = deserializedList.Select(item => item.Personpost.Fodelse.HemortSverige.FodelselanKod).ToList();
//var Fodelseforsamling = deserializedList.Select(item => item.Personpost.Fodelse.HemortSverige.Fodelseforsamling).ToList();
var MedborgarskapslandKod = deserializedList.Select(item => item.Personpost.Medborgarskap.MedborgarskapslandKod).ToList();
var Medborgarskapsdatum = deserializedList.Select(item => item.Personpost.Medborgarskap.Medborgarskapsdatum).ToList();
//var zipped = PersonalIdentityNumber.Zip(FirstName, Tuple.Create);
// foreach (var tuple in zipped)
// {
// //Console.WriteLine(tuple.Item1);
// //Console.WriteLine(tuple.Item2);
// }
//Console.WriteLine(PersonalIdentityNumber.Count());
//for (int i = 0; i < PersonalIdentityNumber.Count; i++)
//{
// var personnummer = PersonalIdentityNumber[i];
// var fornamn = FirstName[i];
// var efternamn = LastName[i];
// var lankod = LanKod[i];
// var kommunkod = Kommunkod[i];
// var utdelAdress2 = UtdelAdress2[i];
// var postNr = PostNr[i];
// var postOrt = PostOrt[i];
// var fodelselanKod = FodelselanKod[i];
// var fodelseforsamling = Fodelseforsamling[i];
// var medborgarskapslandKod = MedborgarskapslandKod[i];
// var medborgarskapsdatum = Medborgarskapsdatum[i];
// use your variables
//}
//Console.WriteLine(PersonalIdentityNumber.Count());
//foreach (var i in PersonalIdentityNumber)
//{
// Console.WriteLine("Personnummer: " + i);
// foreach(var a in FirstName)
// {
// Console.WriteLine("Namn: " + a);
// }
string connetionString = null;
SqlDataAdapter adpter = new SqlDataAdapter();
DataSet ds = new DataSet();
XmlReader xmlFile;
connetionString = "Data Source=tsrv2062;Initial Catalog=Bums;User ID=BumsUser;Password=2tusen7Bums";
xmlFile = XmlReader.Create("navetout.xml", new XmlReaderSettings());
ds.ReadXml(xmlFile);
using (var connection = new SqlConnection(connetionString))
{
connection.Open();
DateTime datum = DateTime.Now;
SqlCommand command1 = new SqlCommand("UPDATE Seamen SET FirstName = @FirstName, LastName = @LastName, NationalRegistrationCountyCode = @NationalRegistrationCountyCode, NationalRegistrationMunicipalityCode = @NationalRegistrationMunicipalityCode, CitizenshipCode = @CitizenshipCode, LastChangedDate = @LastChangedDate WHERE PersonalIdentityNumber = @PersonalIdentityNumber", connection);
for (int i = 0; i < PersonalIdentityNumber.Count; i++)
{
var personnummer = PersonalIdentityNumber[i];
var fornamn = FirstName[i];
var efternamn = LastName[i];
var lankod = LanKod[i];
var kommunkod = Kommunkod[i];
var utdelAdress2 = UtdelAdress2[i];
var postNr = PostNr[i];
//var postOrt = PostOrt[i];
//var fodelselanKod = FodelselanKod[i];
//var fodelseforsamling = Fodelseforsamling[i];
var medborgarskapslandKod = MedborgarskapslandKod[i];
// var medborgarskapsdatum = Medborgarskapsdatum[i];
command1.Parameters.Clear();
command1.Parameters.AddWithValue("@PersonalIdentityNumber", personnummer);
command1.Parameters.AddWithValue("@FirstName", fornamn);
command1.Parameters.AddWithValue("@LastName", efternamn);
command1.Parameters.AddWithValue("@NationalRegistrationCountyCode", lankod);
command1.Parameters.AddWithValue("@NationalRegistrationMunicipalityCode", kommunkod);
command1.Parameters.AddWithValue("@NationalRegistrationDistributionAddress2", utdelAdress2);
//command1.Parameters.AddWithValue("@NationalRegistrationPostCode", postNr);
//command1.Parameters.AddWithValue("@NationalRegistrationCity", postOrt);
//command1.Parameters.AddWithValue("@BirthCountyCode", fodelselanKod);
//command1.Parameters.AddWithValue("@BirthParish", fodelseforsamling);
command1.Parameters.AddWithValue("@CitizenshipCode", medborgarskapslandKod);
// command1.Parameters.AddWithValue("@CitizenshipDate", medborgarskapsdatum);
command1.Parameters.AddWithValue("@LastChangedDate", datum);
command1.ExecuteNonQuery();
Console.WriteLine(personnummer);
}
}
Console.WriteLine("Done");
}
//}// Put a break-point here, then mouse-over PersonalIdentityNumber... deserializedList contains everything if you need it
// catch (Exception)
// {
// throw;
// }
// Console.ReadKey();
//}
private static T Deserialize<T>() where T : new()
{
// Create an instance of T
T ReturnListOfT = CreateInstance<T>();
// Create a new file stream for reading the XML file
using (FileStream ReadFileStream = new FileStream("navetout.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Construct a XmlSerializer and use it
// to serialize the data from the stream.
XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
try
{
// Deserialize the hashtable from the file
ReturnListOfT = (T)SerializerObj.Deserialize(ReadFileStream);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
}
}
// return the Deserialized data.
return ReturnListOfT;
}
// function to create instance of T
public static T CreateInstance<T>() where T : new()
{
return (T)Activator.CreateInstance(typeof(T));
}
答案 0 :(得分:3)
位移不适用于浮点值,因为位的布局不同。他们必须保留小数位和数字(因此浮动&#34;点&#34;值)。
另一方面,整数对于位移很有效,因为它从十进制到二进制的映射程度,但不会在任何地方存储小数点。因此,在施法时,你会丢失这些信息。
简而言之,不可能使用与位整数相同的方式直接乘以十进制值。
但是,您可以将浮点乘以10,直到所有数字都在小数的左侧,然后转换为整数。它可能会影响性能,具体取决于它的实现方式,但以这种方式保存所有信息当然是可能的。如果不理解你的意图,很难回答这个问题。