LoginSignup
0
0

More than 3 years have passed since last update.

【VB】整数以外に\演算子(バックスラッシュ)をつかってはいけない

Posted at

結論

基本的に\を使わない1
代わりにInt等を使う

環境

おそらくVB6.0

発端

VBのコードを見てたら以下のような関数を見かけた

' 100で切り捨てる
Public Function func(cur As Decimal) As Integer
  func = (cur \ 100) * 100
End Function

これだと99.5は100が返ってくる

func(99.49) ' 0
func(99.5)  ' 100

解決

めんどくさがらずにInt等の切り捨て関数を使う2

' 100で切り捨てる
Public Function func2(cur As Decimal) As Integer
  func2 = Int(cur / 100) * 100
End Function
func(99.99) ' 0
func(100)   ' 100

実際に動くコード

(VB) | ブラウザでプログラミング・実行ができる「オンライン実行環境」| paiza.IO
https://paiza.io/projects/HF3vtAn_qajYGVNKz93FpA


  1. \ Operator (Visual Basic) | Microsoft Docs 

  2. 切り捨て関数もいろいろあるがマイナスの値が無いなら深く考えなくて良い 

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0