Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 11th, 2010, 02:43 AM
Registered User
 
Join Date: Feb 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question Can Any One Check This Code Please

This is a Fahrenheit calculation program below

bool flag;
flaot tc,tg;

flag=float.TryParse(txtbox1.Text,out tc);
if(falg==false)
{
messagebox.show("Invalid Entry");
}
txtbox1.Focus();
return;
tg=(5/9)*(tc-32);
txtbox2.Text=tg.tostring();

when i run this program i am getting the result as "0"(i had taken tc=123 as a value)

NOTE: But If I Use Double Instead Of Float I Am Getting The Answer.Why Cant Float Get Answer instead Of "0".
 
Old February 11th, 2010, 05:39 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

(5/9) is int and (tc-32) is float
This is due to implicit conversion from float to int
http://msdn.microsoft.com/en-us/library/y5b434w4.aspx
For example:

You will get value if you calculate as follows:
Code:
((5f / 9f) * (tc - 32));
__________________
Om Prakash Pant
Click the "Thanks" button if this post helped you.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Pls Help Me Check This Code - Win32 ck C++ Programming 4 January 13th, 2005 12:14 AM
Plz Help Me Check What's Wrong Of This Code ! ck C++ Programming 1 January 13th, 2005 12:12 AM
Pls Help Me Check This Code - Win32 ck Visual C++ 0 December 29th, 2004 12:50 PM
check my code nadia Dreamweaver (all versions) 4 October 21st, 2004 12:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.