473,763 Members | 5,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Math.Round() function doesn't appear to be working :|

2 New Member
Hi, i'm currently working on C# project and I want to make use of the Math.Round() function to display only the first to digits of a float value. Unfortunately this doesn't seem to be working for me. I haven't much experience in using the Timer class or the Math class. I may be overlooking something stupid and the solution is probably really simple (happens a lot to me).. here's the piece of code..

Expand|Select|Wrap|Line Numbers
  1. //Retrieve time and round it off.
  2.             float test = 5.123f;
  3.             _t += _tempTimer.Interval;
  4.             Math.Round(test, 2);
  5.  
  6.             //Draw game time text.
  7.  
  8.             e.Graphics.DrawString("Secs: " + test,
  9.                 new Font(new FontFamily("Lucida Console"), 8.0f),
  10.                 new SolidBrush(Color.White), new PointF(300, 10));
  11.  
..using the above, the "test" or the "_t" values do not round off. Anybody have any ideas on why? Thanks in advance.
Dec 21 '10 #1
1 3052
GaryTexmo
1,501 Recognized Expert Top Contributor
http://msdn.microsoft.com/en-us/library/3s2d3xkk.aspx

Math.Round returns the rounded value, it doesn't change the value of the variable it is supplied with.

You can either assign the rounded value to temporary storage, or just use it in your DrawString method :)
Dec 21 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
23513
by: ORC | last post by:
Is there a round function that do a "Normal" rounding and not the round to nearest as the Math.Rounds is? Math.Round(3.44, 1); //Returns 3.4. Math.Round(3.45, 1); //Returns 3.4. Math.Round(3.46, 1); //Returns 3.5. but what I am looking for is a round function that returns 3.5 when rounding the value 3.45 in the above. By the way The ToString does a normal rounding - thank you Microsoft!
6
7098
by: ng_mr | last post by:
No, not a question about "banker's rounding" or whatever it's called. I want to round a double to the nearest 100th, so I perform the following: // original is a double double result = Math.Round( original, 2 ); But I'm not happy with the results. E.g. if original is
17
5659
by: nomenklatura | last post by:
Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then x=3.24 , result is is false
9
7385
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something different. Programming Microsoft Windows with Microsoft Visual Basic.NET "The Round method with a single argument return the whole number nearest to the argument. If the argument to Round is midway between two whole numbers,
7
2452
by: bravesplace | last post by:
Hello, I am using the folling funtion to round a number to a single digit on my form: function round1(num) { return Math.round(num*1)/1 }
4
3446
by: Chris Davoli | last post by:
The folllowing will round to 526, but it should round to 527. It works correctly for all other numbers, except for this one. Does anybody know of a bug in Math.Round? Dim ldecWater As Decimal = 526.5 CType(Math.Round(ldecWater, 0), String) -- Chris Davoli
3
5348
by: Altman | last post by:
OK I was having rounding problems before and I didn't realize that their was a third parameter in the round function that would tell it if it a 5 to round up. I thought adding this would fix the problem but it didn't. I have VS 2005 and I am using vb.net try rounding the number 4.935 to 2 decimals: What I am seeing is the following: Math.Round(4.935, 2, MidpointRounding.AwayFromZero) 'Result is 4.93 Math.Round(4.935, 2,...
4
10894
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
9
5617
by: DaiOz | last post by:
Hi guys im doing a course which includes some JavaScript and I need help with the following question please, The code I have so far is below but I don't think its right please help as I need to complete this part to move onto the next. <HTML> <HEAD> <TITLE>M150 TMA 5 : Programming : Task 1 - Testing Math.random()</TITLE>
0
9387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.