0 Replies - 332 Views - Last Post: 13 June 2014 - 03:11 PM

#1 Oppemaj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 89
  • Joined: 15-November 13

Display decimals without rounding

Posted 13 June 2014 - 03:11 PM

An OP posted a thread asking how he could get rid of all decimals and keep just the first decimal digit without rounding down or up.
Finally, he used substring...

See:
http://www.dreaminco...from-substring/

The solution I want to share with you is fairly simple. I'll break it into steps:

1 Multiply the number by 10 (to keep one decimal digit)
2 Drop the rest of decimals.
3 Divide by 10.

Note: This can be done to display any number of decimals(two decimals, three,...)

Option Explicit On
Option Strict On
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim number As Double = 35.735
        'Display the number with one decimal digit without rounding.
        number = CDbl(Fix(number * 10) / 10)
        'The output is 35.7
    End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Page 1 of 1