Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
Single.cs
Go to the documentation of this file.
1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 using System;
23 using System.Globalization;
24 
25 namespace Lucene.Net.Support
26 {
27  /// <summary>
28  ///
29  /// </summary>
30  public class Single
31  {
32  /// <summary>
33  ///
34  /// </summary>
35  /// <param name="s"></param>
36  /// <param name="style"></param>
37  /// <param name="provider"></param>
38  /// <returns></returns>
39  public static System.Single Parse(System.String s, System.Globalization.NumberStyles style, System.IFormatProvider provider)
40  {
41  if (s.EndsWith("f") || s.EndsWith("F"))
42  return System.Single.Parse(s.Substring(0, s.Length - 1), style, provider);
43  else
44  return System.Single.Parse(s, style, provider);
45  }
46 
47  /// <summary>
48  ///
49  /// </summary>
50  /// <param name="s"></param>
51  /// <param name="provider"></param>
52  /// <returns></returns>
53  public static System.Single Parse(System.String s, System.IFormatProvider provider)
54  {
55  if (s.EndsWith("f") || s.EndsWith("F"))
56  return System.Single.Parse(s.Substring(0, s.Length - 1), provider);
57  else
58  return System.Single.Parse(s, provider);
59  }
60 
61  /// <summary>
62  ///
63  /// </summary>
64  /// <param name="s"></param>
65  /// <param name="style"></param>
66  /// <returns></returns>
67  public static System.Single Parse(System.String s, System.Globalization.NumberStyles style)
68  {
69  if (s.EndsWith("f") || s.EndsWith("F"))
70  return System.Single.Parse(s.Substring(0, s.Length - 1), style);
71  else
72  return System.Single.Parse(s, style);
73  }
74 
75  /// <summary>
76  ///
77  /// </summary>
78  /// <param name="s"></param>
79  /// <returns></returns>
80  public static System.Single Parse(System.String s)
81  {
82  if (s.EndsWith("f") || s.EndsWith("F"))
83  return System.Single.Parse(s.Substring(0, s.Length - 1).Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
84  else
85  return System.Single.Parse(s.Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
86  }
87 
88  public static bool TryParse(System.String s, out float f)
89  {
90  bool ok = false;
91 
92  if (s.EndsWith("f") || s.EndsWith("F"))
93  ok = System.Single.TryParse(s.Substring(0, s.Length - 1).Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator), out f);
94  else
95  ok = System.Single.TryParse(s.Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator), out f);
96 
97  return ok;
98  }
99 
100  /// <summary>
101  ///
102  /// </summary>
103  /// <param name="f"></param>
104  /// <returns></returns>
105  public static string ToString(float f)
106  {
107  return f.ToString().Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, ".");
108  }
109 
110  /// <summary>
111  ///
112  /// </summary>
113  /// <param name="f"></param>
114  /// <param name="format"></param>
115  /// <returns></returns>
116  public static string ToString(float f, string format)
117  {
118  return f.ToString(format).Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, ".");
119  }
120 
121  public static int FloatToIntBits(float value)
122  {
123  return BitConverter.ToInt32(BitConverter.GetBytes(value), 0);
124  }
125 
126  public static float IntBitsToFloat(int value)
127  {
128  return BitConverter.ToSingle(BitConverter.GetBytes(value), 0);
129  }
130  }
131 }