• 0

[VB.NET]Truncate without rounding


Question

So lets say I have a number, 16.95. I want it truncated to one decimal place, but NO ROUNDING. I want 16.9 for the final result.

So far I'm using:

Dim first As Int32 = Math.Floor(tp)
Dim second As Int32 = Int((tp - first) * 10)
Dim TPs As String = first & "." & second

It works for the following example, but not every time. I think only when there's already one decimal place. Another example:

tp = 16.7

first = 16

tp - first = 6.9999885559082 <---- do not understand

TP is a single, if that matters. I'm having a problem getting that one digit in the tenths place.

Link to comment
https://www.neowin.net/forum/topic/643336-vbnettruncate-without-rounding/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Dim tp As Single = 16.95

'Fix simply truncates the decimal whereas Math.Floor always rounds down
'  (e.g. Math.Floor(-16.95) = -17, but Fix(-16.95) = -16)
Dim first As Int32 = Fix(tp)
Dim second As Int32 = Int((tp - first) * 10)
Dim TPs As String = first &amp; "." &amp; second

tp = 16.95

first = 16

second = Int((16.95 - 16) * 10) = Int((.95) * 10) = Int(9.5) = 9

TPs = 16.9

For the record, it could be floating-point rounding errors. You might try using a Double rather than a Single.

Fix truncates. Int rounds down to the nearest integer less than or equal to itself, same as Math.floor IIRC. Other things like CInt and Round all use Banker's Rounding, which is a bit odd for some people... If you have a fractional part of .5 or less, the result rounds toward the nearest even integer. Otherwise, the result is rounded to the nearest integer.

Examples:

Using CInt on a value in the range -0.5 to 0.5 results in 0.

Using CInt on a value in the range -1.499999... to -0.500...001 or 0.500...001 to 1.499999... results in -1 or 1, depending on the sign.

Using CInt on a value in the range -2.5 to -1.5 or 1.5 to 2.5 results in -2 or 2, depending on the sign.

Using CInt on a value in the range -3.499999... to -2.500...001 or 2.500...001 to 3.499999... results in -3 or 3, depending on the sign.

Simply put, Banker's Rounding relies on that extra minute fractional piece to round to the appropriate integer. This is why I use Fix for everything - I don't have any issues with negative v.s. positive values like I would with Int or Math.floor or Math.ceiling or whatever. I also don't have rounding issues like with Round and CInt. Everything is predictable, in other words. Of course, the built-in Math.Round does a wonderful job of rounding "the normal way" already, so I suppose my reinventing the wheel using Fix is rather pointless. ^_^

Edited by rpgfan
  • 0

Truncate to 1 decimal place, no rounding:

Dim decimalNumber As Double = 16.95
Dim truncatedNumber As Double = Math.Truncate(decimalNumber * 10) / 10

Should work. Math.Truncate() gets the integral part of the number (the bit before the decimal point). Therefore, if you multiply that by 10, then truncate, and then divide after, you'll get the one decimal place without any rounding.

  • 0
  GSDragoon said:
So lets say I have a number, 16.95. I want it truncated to one decimal place, but NO ROUNDING. I want 16.9 for the final result.

tp = 16.7

first = 16

tp - first = 6.9999885559082 <---- do not understand

Single and double precision numbers are inherently approximative. It's very dangerous with such numbers, for instance, to compare 7 with 7 for equality: one could be 6.999999999 and the other 7.00000000001.

If you need exactitude, use integers only. Define a type that holds the number as an integer and a power of 10. But in most cases you should be fine with the built-in floats, doubles and decimals.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Half the problem was that they had the damn MSN team as part of Windows for some dumb reason. Now that's finally been changed and we're seeing the effects with you finally being able to turn off the recommended section in the start menu.
    • It hasn't been a decade? Not really. The Settings app in 11 isn't the same as the one in 10. Sure the name is the same but they, probably, redid it all over again, not just changed the top UI layer. I think the old one in Win10 is coded in a way that made it a chore to move more complicated things over. Since the change to the one in Win11, they've actually started move things over and adding things a bit quicker. The fact is that some of the more advanced settings/options are low on the list because few users bother with them compared to the basic ones everyone will go through at some point, like changing the background for example.
    • Free Download Manager 6.28.1.6321 by Razvan Serea Free Download Manager is a powerful, easy-to-use and absolutely free download accelerator and manager. FDM accelerates downloads by splitting files into sections and then downloading them simultaneously. As a result download speed increases up to 600%, or even more! FDM can also resume broken downloads so you needn`t start downloading from the beginning after casual interruption. FDM lets you download files and whole web sites from any remote server via HTTP, HTTPS and FTP. You can also download files using BitTorrent protocol. In addition, Free Download Manager allows you to: adjust traffic usage; to organize and schedule downloads; download video from video sites; download whole web sites with HTML Spider; operate the program remotely, via the internet, and more! Free Download Manager is compatible with the most popular browsers Google Chrome, Firefox, Microsoft Edge, Internet Explorer and Safari. Free Download Manager 6.28.1.6321 changelog: Improved add-ons support. Improved M3U support. Fixed: crash bug in BitTorrent module. Fixed: minor bugs. Windows: a bit improved installer. Windows: Firefox bug workaround. Android: Qt updated to 6.9.1. Download: Free Download Manager (64-bit) | 45.8 MB (Freeware) Links: Home Page | Linux, Mac, Android | MS Store | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Tariffs have nothing to do with this pricing. It was always intended to be slightly more expensive then the S25+
    • Hello, The static link still downloads 10.3.2040.0 from May 22, 2025. The 10.3.2412.0 version can be downloaded directly from emclient.com/dist/v10.3.2412/setup.msi. Regards, Aryeh Goretsky
  • Recent Achievements

    • Contributor
      GravityDead went up a rank
      Contributor
    • Week One Done
      BlakeBringer earned a badge
      Week One Done
    • Week One Done
      Helen Shafer earned a badge
      Week One Done
    • First Post
      emptyother earned a badge
      First Post
    • Week One Done
      Crunchy6 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      662
    2. 2
      ATLien_0
      269
    3. 3
      Michael Scrip
      236
    4. 4
      Steven P.
      164
    5. 5
      +FloatingFatMan
      149
  • Tell a friend

    Love Neowin? Tell a friend!